Skip to content

Commit

Permalink
fix(windows): fix a few more tests (#8644)
Browse files Browse the repository at this point in the history
* fix regression tests

* fix fs.test.ts bigintstats

* enable transpiler cache lol

* remove failing

* fix filesystem router

* update

* fix run-unicode test

* update comment

* add updated snapshot

* fix remaining node-module-module tests

* fixup

* [autofix.ci] apply automated fixes

* fix tty tests

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
dylan-conway and autofix-ci[bot] authored Feb 3, 2024
1 parent eedf008 commit 5934b17
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 82 deletions.
22 changes: 19 additions & 3 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,30 @@ pub fn openInEditor(
}

pub fn getPublicPath(to: string, origin: URL, comptime Writer: type, writer: Writer) void {
return getPublicPathWithAssetPrefix(to, VirtualMachine.get().bundler.fs.top_level_dir, origin, VirtualMachine.get().bundler.options.routes.asset_prefix_path, comptime Writer, writer);
return getPublicPathWithAssetPrefix(
to,
VirtualMachine.get().bundler.fs.top_level_dir,
origin,
VirtualMachine.get().bundler.options.routes.asset_prefix_path,
comptime Writer,
writer,
.loose,
);
}

pub fn getPublicPathWithAssetPrefix(to: string, dir: string, origin: URL, asset_prefix: string, comptime Writer: type, writer: Writer) void {
pub fn getPublicPathWithAssetPrefix(
to: string,
dir: string,
origin: URL,
asset_prefix: string,
comptime Writer: type,
writer: Writer,
comptime platform: bun.path.Platform,
) void {
const relative_path = if (strings.hasPrefix(to, dir))
strings.withoutTrailingSlash(to[dir.len..])
else
VirtualMachine.get().bundler.fs.relative(dir, to);
VirtualMachine.get().bundler.fs.relativePlatform(dir, to, platform);
if (origin.isAbsolute()) {
if (strings.hasPrefix(relative_path, "..") or strings.hasPrefix(relative_path, "./")) {
writer.writeAll(origin.origin) catch return;
Expand Down
3 changes: 3 additions & 0 deletions src/bun.js/api/filesystem_router.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub const FileSystemRouter = struct {
};

this.arena.deinit();
this.router.deinit();
globalThis.allocator().destroy(this.arena);

this.arena = arena;
Expand Down Expand Up @@ -380,6 +381,7 @@ pub const FileSystemRouter = struct {
dir.deref();
}

this.router.deinit();
this.arena.deinit();
}
};
Expand Down Expand Up @@ -586,6 +588,7 @@ pub const MatchedRoute = struct {
if (this.asset_prefix) |prefix| prefix.slice() else "",
@TypeOf(&writer),
&writer,
.posix,
);
return ZigString.init(buf[0..writer.context.pos])
.withEncoding()
Expand Down
3 changes: 3 additions & 0 deletions src/bun.js/bindings/CommonJSModuleRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ JSC_DEFINE_HOST_FUNCTION(functionCommonJSModuleRecord_compile, (JSGlobalObject *
moduleObject->sourceCode.set(vm, moduleObject, jsSourceCode);

auto index = filenameString.reverseFind(PLATFORM_SEP, filenameString.length());
// filenameString is coming from js, any separator could be used
if (index == WTF::notFound)
index = filenameString.reverseFind(NOT_PLATFORM_SEP, filenameString.length());
String dirnameString;
if (index != WTF::notFound) {
dirnameString = filenameString.substring(0, index);
Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/bindings/PathInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#if OS(WINDOWS)
#define PLATFORM_SEP_s "\\"_s
#define PLATFORM_SEP '\\'
#define NOT_PLATFORM_SEP_s "/"_s
#define NOT_PLATFORM_SEP '/'
#else
#define PLATFORM_SEP_s "/"_s
#define PLATFORM_SEP '/'
#define NOT_PLATFORM_SEP_s "\\"_s
#define NOT_PLATFORM_SEP '\\'
#endif

ALWAYS_INLINE bool isAbsolutePath(WTF::String input)
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4031,7 +4031,7 @@ pub const JSValue = enum(JSValueReprInt) {
return jsNumberFromInt32(@as(i32, @intCast(i)));
}

return jsNumberFromDouble(@as(f64, @floatFromInt(@as(i52, @truncate(i)))));
return jsNumberFromDouble(@floatFromInt(i));
}

pub inline fn toJS(this: JSValue, _: *const JSGlobalObject) JSValue {
Expand All @@ -4047,7 +4047,7 @@ pub const JSValue = enum(JSValueReprInt) {
}

pub fn jsNumberFromPtrSize(i: usize) JSValue {
return jsNumberFromDouble(@as(f64, @floatFromInt(@as(i52, @intCast(@as(u51, @truncate(i)))))));
return jsNumberFromDouble(@floatFromInt(i));
}

pub fn coerceDoubleTruncatingIntoInt64(this: JSValue) i64 {
Expand Down
4 changes: 3 additions & 1 deletion src/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ pub fn getRelease(buf: []u8) []const u8 {
if (err != 0) {
return "unknown";
}
return bun.sliceTo(&info.version, 0);
const release = bun.sliceTo(&info.release, 0);
@memcpy(buf[0..release.len], release);
return buf[0..release.len];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/feature_flags.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@ pub const concurrent_transpiler = !env.isWindows;
// https://github.com/oven-sh/bun/issues/5426#issuecomment-1813865316
pub const disable_auto_js_to_ts_in_node_modules = true;

pub const runtime_transpiler_cache = !env.isWindows;
pub const runtime_transpiler_cache = true;
9 changes: 9 additions & 0 deletions src/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@ pub const FileSystem = struct {
});
}

pub fn relativePlatform(_: *@This(), from: string, to: string, comptime platform: path_handler.Platform) string {
return @call(.always_inline, path_handler.relativePlatform, .{
from,
to,
platform,
false,
});
}

pub fn relativeTo(f: *@This(), to: string) string {
return @call(.always_inline, path_handler.relative, .{
f.top_level_dir,
Expand Down
43 changes: 33 additions & 10 deletions src/resolver/resolve_path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ fn windowsVolumeNameLenT(comptime T: type, path: []const T) struct { usize, usiz
// with drive letter
const c = path[0];
if (path[1] == ':') {
if ('a' <= c and c <= 'z' or 'A' <= c and c <= 'Z') {
if (isDriveLetterT(T, c)) {
return .{ 2, 0 };
}
}
Expand Down Expand Up @@ -578,15 +578,27 @@ pub fn windowsFilesystemRoot(path: []const u8) []const u8 {
return windowsFilesystemRootT(u8, path);
}

pub fn isDriveLetter(c: u8) bool {
return isDriveLetterT(u8, c);
}

pub fn isDriveLetterT(comptime T: type, c: T) bool {
return 'a' <= c and c <= 'z' or 'A' <= c and c <= 'Z';
}

// path.relative lets you do relative across different share drives
pub fn windowsFilesystemRootT(comptime T: type, path: []const T) []const T {
if (path.len < 3)
// minimum: `C:`
if (path.len < 2)
return if (isSepAnyT(T, path[0])) path[0..1] else path[0..0];
// with drive letter
const c = path[0];
if (path[1] == ':' and isSepAnyT(T, path[2])) {
if ('a' <= c and c <= 'z' or 'A' <= c and c <= 'Z') {
return path[0..3];
if (path[1] == ':') {
if (isDriveLetterT(T, c)) {
if (path.len > 2 and isSepAnyT(T, path[2]))
return path[0..3]
else
return path[0..2];
}
}
// UNC
Expand Down Expand Up @@ -759,7 +771,7 @@ pub fn normalizeStringGenericT(

if (preserve_trailing_slash) {
// Was there a trailing slash? Let's keep it.
if (buf_i > 0 and path_[path_.len - 1] == separator and buf[buf_i] != separator) {
if (buf_i > 0 and path_[path_.len - 1] == separator and buf[buf_i - 1] != separator) {
buf[buf_i] = separator;
buf_i += 1;
}
Expand Down Expand Up @@ -1004,7 +1016,7 @@ pub fn normalizeStringBuf(
buf: []u8,
comptime allow_above_root: bool,
comptime _platform: Platform,
comptime preserve_trailing_slash: anytype,
comptime preserve_trailing_slash: bool,
) []u8 {
return normalizeStringBufT(u8, str, buf, allow_above_root, _platform, preserve_trailing_slash);
}
Expand All @@ -1015,7 +1027,7 @@ pub fn normalizeStringBufT(
buf: []T,
comptime allow_above_root: bool,
comptime _platform: Platform,
comptime preserve_trailing_slash: anytype,
comptime preserve_trailing_slash: bool,
) []T {
const platform = comptime _platform.resolve();

Expand Down Expand Up @@ -2114,15 +2126,26 @@ export fn ResolvePath__joinAbsStringBufCurrentPlatformBunString(
pub fn platformToPosixInPlace(comptime T: type, path_buffer: []T) void {
if (std.fs.path.sep == '/') return;
var idx: usize = 0;
while (std.mem.indexOfScalarPos(T, path_buffer, idx, std.fs.path.sep)) |index| : (idx = index) {
while (std.mem.indexOfScalarPos(T, path_buffer, idx, std.fs.path.sep)) |index| : (idx = index + 1) {
path_buffer[index] = '/';
}
}

pub fn platformToPosixBuf(comptime T: type, path: []const T, buf: []T) []T {
if (std.fs.path.sep == '/') return;
var idx: usize = 0;
while (std.mem.indexOfScalarPos(T, path, idx, std.fs.path.sep)) |index| : (idx = index + 1) {
@memcpy(buf[idx..index], path[idx..index]);
buf[index] = '/';
}
@memcpy(buf[idx..path.len], path[idx..path.len]);
return buf[0..path.len];
}

pub fn posixToPlatformInPlace(comptime T: type, path_buffer: []T) void {
if (std.fs.path.sep == '/') return;
var idx: usize = 0;
while (std.mem.indexOfScalarPos(T, path_buffer, idx, '/')) |index| : (idx = index) {
while (std.mem.indexOfScalarPos(T, path_buffer, idx, '/')) |index| : (idx = index + 1) {
path_buffer[index] = std.fs.path.sep;
}
}
22 changes: 16 additions & 6 deletions src/resolver/resolver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3277,7 +3277,17 @@ pub const Resolver = struct {
defer sliced.deinit();

const str = brk: {
if (std.fs.path.isAbsolute(sliced.slice())) break :brk sliced.slice();
if (std.fs.path.isAbsolute(sliced.slice())) {
if (comptime Environment.isWindows) {
const dir_path_buf = bufs(.node_modules_paths_buf);
var normalizer = bun.path.PosixToWinNormalizer{};
const normalized = normalizer.resolveCWD(sliced.slice()) catch {
@panic("Failed to get cwd for _nodeModulesPaths");
};
break :brk bun.path.normalizeBuf(normalized, dir_path_buf, .windows);
}
break :brk sliced.slice();
}
const dir_path_buf = bufs(.node_modules_paths_buf);
break :brk bun.path.joinStringBuf(dir_path_buf, &[_]string{ r.fs.top_level_dir, sliced.slice() }, .auto);
};
Expand All @@ -3292,10 +3302,10 @@ pub const Resolver = struct {
const path_without_trailing_slash = strings.withoutTrailingSlash(dir_info.abs_path);
const path_parts = brk: {
if (path_without_trailing_slash.len == 1 and path_without_trailing_slash[0] == '/') {
break :brk [2]string{ "", "/node_modules" };
break :brk [2]string{ "", std.fs.path.sep_str ++ "node_modules" };
}

break :brk [2]string{ path_without_trailing_slash, "/node_modules" };
break :brk [2]string{ path_without_trailing_slash, std.fs.path.sep_str ++ "node_modules" };
};
const nodemodules_path = bun.strings.concat(stack_fallback_allocator.get(), &path_parts) catch unreachable;
bun.path.posixToPlatformInPlace(u8, nodemodules_path);
Expand All @@ -3305,7 +3315,7 @@ pub const Resolver = struct {
} else {
// does not exist
const full_path = std.fs.path.resolve(r.allocator, &[1][]const u8{str}) catch unreachable;
var path = full_path;
var path = strings.withoutTrailingSlash(full_path);
while (true) {
const path_without_trailing_slash = strings.withoutTrailingSlash(path);

Expand All @@ -3315,13 +3325,13 @@ pub const Resolver = struct {
stack_fallback_allocator.get(),
&[_]string{
path_without_trailing_slash,
"/node_modules",
std.fs.path.sep_str ++ "node_modules",
},
) catch unreachable,
),
) catch unreachable;

path = path[0 .. strings.lastIndexOfChar(path, '/') orelse break];
path = path[0 .. strings.lastIndexOfChar(path, std.fs.path.sep) orelse break];
}
}

Expand Down
Loading

0 comments on commit 5934b17

Please sign in to comment.