From daecb245780669fdfdecf3bf3b39a484493a675b Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Fri, 1 Nov 2024 23:00:21 -0700 Subject: [PATCH] normalize --- src/install/install.zig | 17 ++++++++++++++-- src/resolver/resolve_path.zig | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/install/install.zig b/src/install/install.zig index 7c457914f4..b76be21307 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -8367,7 +8367,13 @@ pub const PackageManager = struct { fn httpThreadOnInitError(err: HTTP.InitError, opts: HTTP.HTTPThread.InitOpts) noreturn { switch (err) { error.LoadCAFile => { - if (!bun.sys.existsZ(opts.abs_ca_file_name)) { + var buf: if (Environment.isWindows) bun.path.PosixToWinNormalizer else void = undefined; + const path = if (comptime Environment.isWindows) + buf.resolveZ(FileSystem.instance.top_level_dir, opts.abs_ca_file_name) + else + opts.abs_ca_file_name; + + if (!bun.sys.existsZ(path)) { Output.err("HTTPThread", "could not find CA file: '{s}'", .{opts.abs_ca_file_name}); } else { Output.err("HTTPThread", "invalid CA file: '{s}'", .{opts.abs_ca_file_name}); @@ -14851,7 +14857,14 @@ pub const PackageManager = struct { const original_path = this_bundler.env.get("PATH") orelse ""; var PATH = try std.ArrayList(u8).initCapacity(bun.default_allocator, original_path.len + 1 + "node_modules/.bin".len + cwd.len + 1); - var current_dir: ?*DirInfo = this_bundler.resolver.readDirInfo(cwd) catch null; + var current_dir: ?*DirInfo = this_bundler.resolver.readDirInfo(cwd) catch |err| { + Output.err(err, "failed to read directory '{s}'", .{cwd}); + Global.crash(); + }; + if (current_dir == null) { + Output.errGeneric("directory does not exist: '{s}'", .{cwd}); + Global.crash(); + } bun.assert(current_dir != null); while (current_dir) |dir| { if (PATH.items.len > 0 and PATH.items[PATH.items.len - 1] != std.fs.path.delimiter) { diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 07909bba74..b450218dd2 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -1902,6 +1902,14 @@ pub const PosixToWinNormalizer = struct { return resolveWithExternalBuf(&this._raw_bytes, source_dir, maybe_posix_path); } + pub inline fn resolveZ( + this: *PosixToWinNormalizer, + source_dir: []const u8, + maybe_posix_path: []const u8, + ) [:0]const u8 { + return resolveWithExternalBufZ(&this._raw_bytes, source_dir, maybe_posix_path); + } + pub inline fn resolveCWD( this: *PosixToWinNormalizer, maybe_posix_path: []const u8, @@ -1943,6 +1951,35 @@ pub const PosixToWinNormalizer = struct { return maybe_posix_path; } + fn resolveWithExternalBufZ( + buf: *Buf, + source_dir: []const u8, + maybe_posix_path: []const u8, + ) [:0]const u8 { + assert(std.fs.path.isAbsoluteWindows(maybe_posix_path)); + if (bun.Environment.isWindows) { + const root = windowsFilesystemRoot(maybe_posix_path); + if (root.len == 1) { + assert(isSepAny(root[0])); + if (bun.strings.isWindowsAbsolutePathMissingDriveLetter(u8, maybe_posix_path)) { + const source_root = windowsFilesystemRoot(source_dir); + @memcpy(buf[0..source_root.len], source_root); + @memcpy(buf[source_root.len..][0 .. maybe_posix_path.len - 1], maybe_posix_path[1..]); + buf[source_root.len + maybe_posix_path.len - 1] = 0; + const res = buf[0 .. source_root.len + maybe_posix_path.len - 1 :0]; + assert(!bun.strings.isWindowsAbsolutePathMissingDriveLetter(u8, res)); + assert(std.fs.path.isAbsoluteWindows(res)); + return res; + } + } + assert(!bun.strings.isWindowsAbsolutePathMissingDriveLetter(u8, maybe_posix_path)); + } + + @memcpy(buf[0..maybe_posix_path.len], maybe_posix_path); + buf[maybe_posix_path.len] = 0; + return buf[0..maybe_posix_path.len :0]; + } + pub fn resolveCWDWithExternalBuf( buf: *Buf, maybe_posix_path: []const u8,