From 27c82a67630c365a44210f75783e7aabe4f6fdc6 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 5 Sep 2023 17:43:23 -0800 Subject: [PATCH] Many more things are starting to work. --- src/__global.zig | 2 +- src/bun.js/module_loader.zig | 1 + src/bun.zig | 3 +-- src/cli/init_command.zig | 22 +++++----------------- src/cli/run_command.zig | 4 ++-- src/main.zig | 5 +++-- src/resolver/resolve_path.zig | 14 ++++++++++++-- src/string_immutable.zig | 4 ++-- src/sys.zig | 31 +++++++++++++++++++++++++------ 9 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/__global.zig b/src/__global.zig index 675da4deaf..28e0c726ab 100644 --- a/src/__global.zig +++ b/src/__global.zig @@ -79,7 +79,7 @@ pub fn setThreadName(name: StringTypes.stringZ) void { /// Flushes stdout and stderr and exits with the given code. pub fn exit(code: u8) noreturn { Output.flush(); - std.os.exit(code); + std.c._exit(code); } pub const AllocatorConfiguration = struct { diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index 277c1592c3..29ed9c4f37 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -155,6 +155,7 @@ const BunDebugHolder = struct { }; fn dumpSource(specifier: string, printer: anytype) !void { + if (comptime Environment.isWindows) return; if (BunDebugHolder.dir == null) { BunDebugHolder.dir = try std.fs.cwd().makeOpenPathIterable("/tmp/bun-debug-src/", .{}); BunDebugHolder.lock = bun.Lock.init(); diff --git a/src/bun.zig b/src/bun.zig index 31f59b1c2a..0d2e492a46 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -774,12 +774,11 @@ pub fn getenvZ(path_: [:0]const u8) ?[]const u8 { // Windows UCRT will fill this in for us for (std.os.environ) |lineZ| { const line = sliceTo(lineZ, 0); - const key_end = strings.indexOfCharUsize(path_, '=') orelse line.len; + const key_end = strings.indexOfCharUsize(line, '=') orelse line.len; const key = line[0..key_end]; if (strings.eqlLong(key, path_, true)) { return line[@min(key_end + 1, line.len)..]; } - continue; } return null; diff --git a/src/cli/init_command.zig b/src/cli/init_command.zig index 8d65461a73..03591984cb 100644 --- a/src/cli/init_command.zig +++ b/src/cli/init_command.zig @@ -23,19 +23,7 @@ const logger = @import("root").bun.logger; const JSPrinter = bun.js_printer; fn exists(path: anytype) bool { - if (@TypeOf(path) == [:0]const u8 or @TypeOf(path) == [:0]u8) { - if (std.os.accessZ(path, 0)) { - return true; - } else |_| { - return false; - } - } else { - if (std.os.access(path, 0)) { - return true; - } else |_| { - return false; - } - } + return bun.sys.exists(path); } pub const InitCommand = struct { fn prompt( @@ -210,7 +198,7 @@ pub const InitCommand = struct { ).data.e_object; } - const auto_yes = brk: { + const auto_yes = Output.stdout_descriptor_type != .terminal or brk: { for (argv) |arg_| { const arg = bun.span(arg_); if (strings.eqlComptime(arg, "-y") or strings.eqlComptime(arg, "--yes")) { @@ -427,9 +415,9 @@ pub const InitCommand = struct { }, alloc, ); - process.stderr_behavior = .Pipe; - process.stdin_behavior = .Pipe; - process.stdout_behavior = .Pipe; + process.stderr_behavior = .Ignore; + process.stdin_behavior = .Ignore; + process.stdout_behavior = .Ignore; _ = try process.spawnAndWait(); } } diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index b8c4b3d508..c2afcdd85c 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -917,13 +917,13 @@ pub const RunCommand = struct { break :brk std.fs.openFileAbsolute(script_name_to_search, .{ .mode = .read_only }); } else { const cwd = bun.getcwd(&path_buf) catch break :possibly_open_with_bun_js; - path_buf[cwd.len] = std.fs.path.sep; + path_buf[cwd.len] = std.fs.path.sep_posix; var parts = [_]string{script_name_to_search}; file_path = resolve_path.joinAbsStringBuf( path_buf[0 .. cwd.len + 1], &path_buf2, &parts, - .auto, + .loose, ); if (file_path.len == 0) break :possibly_open_with_bun_js; path_buf2[file_path.len] = 0; diff --git a/src/main.zig b/src/main.zig index abd303c74e..a46bac26a3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,7 +11,8 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, addr const CrashReporter = @import("./crash_reporter.zig"); -extern "C" var _environ: ?*anyopaque; +pub extern "C" var _environ: ?*anyopaque; +pub extern "C" var environ: ?*anyopaque; pub fn main() void { const bun = @import("root").bun; @@ -21,7 +22,7 @@ pub fn main() void { if (comptime Environment.isRelease) CrashReporter.start() catch unreachable; if (comptime Environment.isWindows) { - std.c.environ = @ptrCast(std.os.environ.ptr); + environ = @ptrCast(std.os.environ.ptr); _environ = @ptrCast(std.os.environ.ptr); } diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 55351459a1..68424e1a87 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -920,8 +920,18 @@ fn _joinAbsStringBuf(comptime is_sentinel: bool, comptime ReturnType: type, _cwd out += part.len; } - const leading_separator: []const u8 = if (_platform.leadingSeparatorIndex(temp_buf[0..out])) |i| - temp_buf[0 .. i + 1] + const leading_separator: []const u8 = if (_platform.leadingSeparatorIndex(temp_buf[0..out])) |i| brk: { + var outdir = temp_buf[0 .. i + 1]; + if (_platform == .windows or _platform == .loose) { + for (outdir) |*c| { + if (c.* == '\\') { + c.* = '/'; + } + } + } + + break :brk outdir; + } else "/"; diff --git a/src/string_immutable.zig b/src/string_immutable.zig index bb6b172e1a..d5fba27b20 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -1488,7 +1488,7 @@ pub fn fromWPath(buf: []u8, utf16: []const u16) [:0]const u8 { return buf[0..encode_into_result.written :0]; } -pub fn toWObjectPath(wbuf: []u16, utf8: []const u8) [:0]const u16 { +pub fn toNTPath(wbuf: []u16, utf8: []const u8) [:0]const u16 { if (!std.fs.path.isAbsoluteWindows(utf8)) { return toWPathNormalized(wbuf, utf8); } @@ -1498,7 +1498,7 @@ pub fn toWObjectPath(wbuf: []u16, utf8: []const u8) [:0]const u16 { } // These are the same because they don't have rules like needing a trailing slash -pub const toWObjectDir = toWObjectPath; +pub const toNTDir = toNTPath; pub fn toWPathNormalized(wbuf: []u16, utf8: []const u8) [:0]const u16 { var renormalized: [bun.MAX_PATH_BYTES]u8 = undefined; diff --git a/src/sys.zig b/src/sys.zig index 72fce02bca..d5711024ea 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -235,7 +235,7 @@ pub fn mkdir(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) { return Maybe(void).errnoSysP(linux.mkdir(file_path, flags), .mkdir, file_path) orelse Maybe(void).success; } var wbuf: bun.MAX_WPATH = undefined; - _ = kernel32.CreateDirectoryW(bun.strings.toWObjectDir(&wbuf, file_path).ptr, null); + _ = kernel32.CreateDirectoryW(bun.strings.toNTDir(&wbuf, file_path).ptr, null); return Maybe(void).errnoSysP(0, .mkdir, file_path) orelse Maybe(void).success; } @@ -380,7 +380,7 @@ pub noinline fn openDirAtWindowsA( no_follow: bool, ) Maybe(bun.FileDescriptor) { var wbuf: bun.MAX_WPATH = undefined; - return openDirAtWindows(dirFd, bun.strings.toWObjectDir(&wbuf, path), iterable, no_follow); + return openDirAtWindows(dirFd, bun.strings.toNTDir(&wbuf, path), iterable, no_follow); } pub fn openatWindows(dirfD: bun.FileDescriptor, path: []const u16, flags: bun.Mode) Maybe(bun.FileDescriptor) { const nonblock = flags & O.NONBLOCK != 0; @@ -531,7 +531,7 @@ pub fn openat(dirfd: bun.FileDescriptor, file_path: [:0]const u8, flags: bun.Mod } var wbuf: bun.MAX_WPATH = undefined; - return openatWindows(dirfd, bun.strings.toWObjectPath(&wbuf, file_path), flags); + return openatWindows(dirfd, bun.strings.toNTPath(&wbuf, file_path), flags); } return openatOSPath(dirfd, file_path, flags, perm); @@ -544,7 +544,7 @@ pub fn openatA(dirfd: bun.FileDescriptor, file_path: []const u8, flags: bun.Mode } var wbuf: bun.MAX_WPATH = undefined; - return openatWindows(dirfd, bun.strings.toWObjectPath(&wbuf, file_path), flags); + return openatWindows(dirfd, bun.strings.toNTPath(&wbuf, file_path), flags); } return openatOSPath(dirfd, std.os.toPosixPath(file_path) catch return Maybe(bun.FileDescriptor){ @@ -1342,6 +1342,25 @@ pub fn existsOSPath(path: bun.OSPathSlice) bool { @compileError("TODO: existsOSPath"); } +pub fn exists(path: []const u8) bool { + if (comptime Environment.isPosix) { + if (comptime @inComptime()) { + return system.access(path, 0) == 0; + } + + + return system.access(&(std.os.toPosixPath(path) catch return false) , 0) == 0; + } + + if (comptime Environment.isWindows) { + var wbuf: bun.MAX_WPATH = undefined; + const path_to_use = bun.strings.toWPath(&wbuf, path); + return kernel32.GetFileAttributesW(path_to_use.ptr) != os.windows.INVALID_FILE_ATTRIBUTES; + } + + @compileError("TODO: existsOSPath"); +} + pub fn isExecutableFileOSPath(path: bun.OSPathSlice) bool { if (comptime Environment.isPosix) { return bun.is_executable_fileZ(path); @@ -1350,7 +1369,7 @@ pub fn isExecutableFileOSPath(path: bun.OSPathSlice) bool { if (comptime Environment.isWindows) { var out: windows.DWORD = 8; const rc = kernel32.GetBinaryTypeW(path, &out); - log("GetBinaryTypeW({}) = {d}", .{ bun.String.init(path), out }); + log("GetBinaryTypeW({}) = {d}", .{ bun.strings.fmtUTF16(path), out }); if (rc == windows.FALSE) { return false; @@ -1447,4 +1466,4 @@ pub fn dup(fd: bun.FileDescriptor) Maybe(bun.FileDescriptor) { const out = std.c.dup(fd); return Maybe(bun.FileDescriptor).errnoSysFd(out, .dup, fd) orelse Maybe(bun.FileDescriptor){ .result = bun.toFD(out) }; -} +} \ No newline at end of file