Skip to content

Commit

Permalink
windows: fix argument handling in bin shim (#9622)
Browse files Browse the repository at this point in the history
* awa

* update v5

* a

* [autofix.ci] apply automated fixes

* tarball

* a

* a

* blah

* [autofix.ci] apply automated fixes

* bump

* wah

* [autofix.ci] apply automated fixes

* farther

* a

* [autofix.ci] apply automated fixes

* im confused

* [autofix.ci] apply automated fixes

* Fix typo (#9623)

* Fix crash in Bun.escapeHTML (#9619)

Co-authored-by: Jarred Sumner <[email protected]>

* Fixes #9610

* Implement `fs.openAsBlob` (#9628)

* Implement `fs.openAsBlob`

* Use a function

---------

Co-authored-by: Jarred Sumner <[email protected]>

* Fix `bun install -g` not working on Docker
Closes #8753

* Revert "Fix `bun install -g` not working on Docker"

This reverts commit 20fce1a.

* what happens if nonblocking tty (#9608)

Co-authored-by: Jarred Sumner <[email protected]>

* merge

* a

* blah

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <[email protected]>
Co-authored-by: sequencerr <[email protected]>
Co-authored-by: Jarred Sumner <[email protected]>
Co-authored-by: Ashcon Partovi <[email protected]>
  • Loading branch information
6 people committed Mar 27, 2024
1 parent 1675349 commit e808cdb
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 112 deletions.
4 changes: 2 additions & 2 deletions docs/installation.md
Expand Up @@ -232,8 +232,8 @@ If you need to remove Bun from your system, use the following commands.
$ rm -rf ~/.bun # for macOS, Linux, and WSL
```

```bash#Windows
$ Remove-Item ~\.bun -Recurse
```powershell#Windows
powershell -c ~\.bun\uninstall.ps1
```

```bash#NPM
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/WebKit
Submodule WebKit updated 1 files
+2 −0 Dockerfile
13 changes: 8 additions & 5 deletions src/cli/run_command.zig
Expand Up @@ -456,7 +456,7 @@ pub const RunCommand = struct {
// wrapper exe. we build the full exe path even though we could do
// a relative lookup, because in the case we do find it, we have to
// generate this full path anyways.
if (Environment.isWindows and bun.strings.hasSuffixComptime(executable, ".exe")) {
if (Environment.isWindows and bun.FeatureFlags.windows_bunx_fast_path and bun.strings.hasSuffixComptime(executable, ".exe")) {
std.debug.assert(std.fs.path.isAbsolute(executable));

// Using @constCast is safe because we know that
Expand Down Expand Up @@ -1506,7 +1506,7 @@ pub const RunCommand = struct {
}

// Run absolute/relative path
if ((script_name_to_search.len > 1 and script_name_to_search[0] == '/') or
if (std.fs.path.isAbsolute(script_name_to_search) or
(script_name_to_search.len > 2 and script_name_to_search[0] == '.' and script_name_to_search[1] == '/'))
{
Run.boot(ctx, ctx.allocator.dupe(u8, script_name_to_search) catch unreachable) catch |err| {
Expand All @@ -1525,7 +1525,6 @@ pub const RunCommand = struct {

if (script_name_to_search.len == 1 and script_name_to_search[0] == '-') {
// read from stdin

var stack_fallback = std.heap.stackFallback(2048, bun.default_allocator);
var list = std.ArrayList(u8).init(stack_fallback.get());
errdefer list.deinit();
Expand Down Expand Up @@ -1554,7 +1553,7 @@ pub const RunCommand = struct {
return true;
}

if (Environment.isWindows) try_bunx_file: {
if (Environment.isWindows and bun.FeatureFlags.windows_bunx_fast_path) try_bunx_file: {
// Attempt to find a ".bunx" file on disk, and run it, skipping the
// wrapper exe. we build the full exe path even though we could do
// a relative lookup, because in the case we do find it, we have to
Expand Down Expand Up @@ -1675,7 +1674,10 @@ pub const BunXFastPath = struct {
var environment_buffer: bun.WPathBuffer = undefined;

/// If this returns, it implies the fast path cannot be taken
fn tryLaunch(ctx: Command.Context, path_to_use: [:0]u16, env: *DotEnv.Loader, passthrough: []const []const u8) void {
fn tryLaunch(ctx_const: Command.Context, path_to_use: [:0]u16, env: *DotEnv.Loader, passthrough: []const []const u8) void {
if (!bun.FeatureFlags.windows_bunx_fast_path) return;

var ctx = ctx_const;
std.debug.assert(bun.isSliceInBufferT(u16, path_to_use, &BunXFastPath.direct_launch_buffer));
var command_line = BunXFastPath.direct_launch_buffer[path_to_use.len..];

Expand All @@ -1700,6 +1702,7 @@ pub const BunXFastPath = struct {
const result = bun.strings.convertUTF8toUTF16InBuffer(command_line[1 + i ..], str);
i += result.len + 1;
}
ctx.passthrough = passthrough;

const run_ctx = shim_impl.FromBunRunContext{
.handle = handle,
Expand Down
9 changes: 6 additions & 3 deletions src/cli/uninstall.ps1
Expand Up @@ -12,7 +12,8 @@ $ErrorActionPreference = "Stop"
function Write-Env {
param([String]$Key, [String]$Value)

$EnvRegisterKey = Get-Item -Path 'HKCU:Environment'
$RegisterKey = Get-Item -Path 'HKCU:'
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
if ($null -eq $Value) {
$EnvRegisterKey.DeleteValue($Key)
} else {
Expand All @@ -30,7 +31,8 @@ function Write-Env {
function Get-Env {
param([String] $Key)

$RegisterKey = Get-Item -Path 'HKCU:Environment'
$RegisterKey = Get-Item -Path 'HKCU:'
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$EnvRegisterKey.GetValue($Key, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
}

Expand Down Expand Up @@ -94,8 +96,9 @@ try {
$Path = $Path -split ';'
$Path = $Path | Where-Object { $_ -ne "${PSScriptRoot}\bin" }
Write-Env -Key 'Path' -Value ($Path -join ';')
} catch {
} catch {
Write-Host "Could not remove ${PSScriptRoot}\bin from PATH."
Write-Error $_
if ($PauseOnError) { pause }
exit 1
}
Expand Down
2 changes: 2 additions & 0 deletions src/feature_flags.zig
Expand Up @@ -164,6 +164,8 @@ pub const disable_auto_js_to_ts_in_node_modules = true;

pub const runtime_transpiler_cache = true;

pub const windows_bunx_fast_path = false; // Disabled to simplify fixing the bunx issue

pub const breaking_changes_1_1_0 = false;

// This causes strange bugs where writing via console.log (sync) has a different
Expand Down
5 changes: 3 additions & 2 deletions src/install/extract_tarball.zig
Expand Up @@ -451,7 +451,7 @@ fn extract(this: *const ExtractTarball, tgz_bytes: []const u8) !Install.ExtractD
/// Opens the dir if the path already exists and is a directory.
/// This function is not atomic, and if it returns an error, the file system may
/// have been modified regardless.
fn makeOpenPathAccessMaskW(self: std.fs.Dir, sub_path: []const u8, access_mask: u32, no_follow: bool) std.os.OpenError!std.fs.Dir {
fn makeOpenPathAccessMaskW(self: std.fs.Dir, sub_path: []const u8, access_mask: u32, no_follow: bool) !std.fs.Dir {
var it = try std.fs.path.componentIterator(sub_path);
// If there are no components in the path, then create a dummy component with the full path.
var component = it.last() orelse std.fs.path.NativeUtf8ComponentIterator.Component{
Expand Down Expand Up @@ -483,7 +483,7 @@ const MakeOpenDirAccessMaskWOptions = struct {
create_disposition: u32,
};

fn makeOpenDirAccessMaskW(self: std.fs.Dir, sub_path_w: [*:0]const u16, access_mask: u32, flags: MakeOpenDirAccessMaskWOptions) std.os.OpenError!std.fs.Dir {
fn makeOpenDirAccessMaskW(self: std.fs.Dir, sub_path_w: [*:0]const u16, access_mask: u32, flags: MakeOpenDirAccessMaskWOptions) !std.fs.Dir {
var result = std.fs.Dir{
.fd = undefined,
};
Expand Down Expand Up @@ -528,6 +528,7 @@ fn makeOpenDirAccessMaskW(self: std.fs.Dir, sub_path_w: [*:0]const u16, access_m
// and the directory is trying to be opened for iteration.
.ACCESS_DENIED => return error.AccessDenied,
.INVALID_PARAMETER => return error.BadPathName,
.SHARING_VIOLATION => return error.SharingViolation,
else => return w.unexpectedStatus(rc),
}
}
6 changes: 0 additions & 6 deletions src/install/semver.zig
Expand Up @@ -63,12 +63,6 @@ pub const String = extern struct {
}
}

pub fn isUndefined(this: *const String) bool {
const num: u64 = undefined;
const bytes = @as(u64, @bitCast(this.bytes));
return @as(u63, @truncate(bytes)) == @as(u63, @truncate(num));
}

pub const Formatter = struct {
str: *const String,
buf: string,
Expand Down
16 changes: 9 additions & 7 deletions src/install/windows-shim/BinLinkingShim.zig
Expand Up @@ -30,7 +30,7 @@ shebang: ?Shebang,
/// These arbitrary numbers will probably not show up in the other fields.
/// This will reveal off-by-one mistakes.
pub const VersionFlag = enum(u13) {
pub const current = .v4;
pub const current = .v5;

v1 = 5474,
/// Fix bug where paths were not joined correctly
Expand All @@ -41,6 +41,8 @@ pub const VersionFlag = enum(u13) {
/// automatic fallback path where if "node" is asked for, but not present,
/// it will retry the spawn with "bun".
v4 = 5477,
/// Fixed bugs where passing arguments did not always work.
v5 = 5478,
_,
};

Expand Down Expand Up @@ -82,14 +84,14 @@ fn wU8(comptime s: []const u8) []const u8 {
pub const Shebang = struct {
launcher: []const u8,
utf16_len: u32,
is_bun: bool,
is_node_or_bun: bool,

pub fn init(launcher: []const u8, is_bun: bool) !Shebang {
pub fn init(launcher: []const u8, is_node_or_bun: bool) !Shebang {
return .{
.launcher = launcher,
// TODO(@paperdave): what if this is invalid utf8?
.utf16_len = @intCast(bun.simdutf.length.utf16.from.utf8(launcher)),
.is_bun = is_bun,
.is_node_or_bun = is_node_or_bun,
};
}

Expand Down Expand Up @@ -200,8 +202,8 @@ pub const Shebang = struct {
if (eqlComptime(first, "/usr/bin/env") or eqlComptime(first, "/bin/env")) {
const rest = tokenizer.rest();
const program = tokenizer.next() orelse return parseFromBinPath(bin_path);
const is_bun = eqlComptime(program, "bun") or eqlComptime(program, "node");
return try Shebang.init(rest, is_bun);
const is_node_or_bun = eqlComptime(program, "bun") or eqlComptime(program, "node");
return try Shebang.init(rest, is_node_or_bun);
}

return try Shebang.init(line, false);
Expand Down Expand Up @@ -235,7 +237,7 @@ pub fn encodeInto(options: @This(), buf: []u8) !void {
wbuf[1] = 0;
wbuf = wbuf[2..];

const is_node_or_bun = if (options.shebang) |s| s.is_bun else false;
const is_node_or_bun = if (options.shebang) |s| s.is_node_or_bun else false;
var flags = Flags{
.has_shebang = options.shebang != null,
.is_node_or_bun = is_node_or_bun,
Expand Down
Binary file modified src/install/windows-shim/bun_shim_impl.exe
Binary file not shown.

0 comments on commit e808cdb

Please sign in to comment.