From 07c6731198251b8ddbbfcca5e1de983ef7d3965d Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Fri, 11 Oct 2024 21:00:46 +0000 Subject: [PATCH 01/18] Fix `anchor build` on Windows --- cli/src/lib.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 52b22d724d..8c0a4eaa0a 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1524,10 +1524,24 @@ fn build_rust_cwd( no_docs: bool, arch: &ProgramArch, ) -> Result<()> { - match cargo_toml.parent() { + let mut cargo_toml_parent = match cargo_toml.parent() { + Some(parent) => parent.to_owned(), None => return Err(anyhow!("Unable to find parent")), - Some(p) => std::env::set_current_dir(p)?, }; + + // build-sbf depends on cargo metadata, which in turn depends on glob + // to find packages in a workspace + // but glob breaks on UNC paths like \\?\C:\...\packages\* + // so on windows, find the relative path of the package and use + // https://github.com/rust-lang/glob/issues/132 + #[cfg(windows)] + { + let workspace_dir = cfg.path().parent().unwrap(); + let relative_path = cargo_toml_parent.strip_prefix(workspace_dir.canonicalize()?)?; + cargo_toml_parent = std::path::absolute(workspace_dir.join(relative_path))?; + } + + std::env::set_current_dir(cargo_toml_parent)?; match build_config.verifiable { false => _build_rust_cwd( cfg, no_idl, idl_out, idl_ts_out, skip_lint, no_docs, arch, cargo_args, @@ -1915,7 +1929,20 @@ fn _build_rust_cwd( arch: &ProgramArch, cargo_args: Vec, ) -> Result<()> { - let exit = std::process::Command::new("cargo") + let mut build_command = std::process::Command::new("cargo"); + + // TODO: fix upstream in solana-build-sbf + #[cfg(windows)] + if std::env::var("HOME").is_err() { + build_command.env( + "HOME", + std::env::var("USERPROFILE").map_err(|e| { + anyhow!("env variable 'HOME' not set and could not read 'USERPROFILE': {e}") + })?, + ); + } + + let exit = build_command .arg(arch.build_subcommand()) .args(cargo_args.clone()) .stdout(Stdio::inherit()) From a31bdbfecd548d8feda672cbe9f26a2b3731077b Mon Sep 17 00:00:00 2001 From: Wilfred Almeida Date: Tue, 15 Oct 2024 16:35:03 +0000 Subject: [PATCH 02/18] feat: windows release ci --- .github/workflows/release.yaml | 105 +++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000000..5e39f45702 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,105 @@ +name: Release + +on: + workflow_dispatch: + push: + branches: [master] + tags: + - "v*.*.*" + +jobs: + build: + strategy: + matrix: + os: + - windows-latest + # - ubuntu-latest + # - macos-latest + # - macos-14 + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Latest Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + default: true + override: true + + # - name: Cache Cargo registry + index + # uses: actions/cache@v3 + # if: ${{ env.CACHE != 'false' }} + # with: + # path: | + # ~/.cargo/bin/ + # ~/.cargo/registry/index/ + # ~/.cargo/registry/cache/ + # ~/.cargo/git/db/ + # ./target/ + # key: cargo-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} + + - name: Set Version + id: set-version + shell: sh + run: | + VERSION=$(printf "%s" "${{ github.ref_name }}" | sed -e "s|/|-|g") + if [ "${{ github.ref_type }}" = "branch" ]; then + VERSION=$(printf "%s" "${GITHUB_SHA}" | cut -c-6) + fi + echo "version: ${VERSION}" + echo "version=${VERSION}" >> "${GITHUB_OUTPUT}" + + - name: Build + id: build + shell: bash + run: | + # build avm and anchor separately for now because + # of the name conflict in avm/anchor and cli/anchor + # cargo build --release -p anchor-cli --bin anchor + # cargo build --release -p avm --bin avm + cargo build --release --bin hello + cp "target/release/hello.exe" "target/release/avm.exe" + cp "target/release/hello.exe" "target/release/anchor.exe" + + binary_extension="" + + if [[ "$RUNNER_OS" == "Linux" ]]; then + release_folder="ubuntu-latest" + fi + if [[ "${RUNNER_OS}" == "Windows" ]]; then + binary_extension=".exe" + release_folder="windows-latest" + fi + if [[ "${RUNNER_OS}" == "macOS" ]]; then + if [[ "${{ matrix.os }}" == "macos-14" ]]; then + release_folder="macos-m1-latest" + else + release_folder="macos-intel-latest" + fi + fi + + mkdir -p "${release_folder}" + echo "::set-output name=release-folder::${release_folder}" + + mv "target/release/anchor${binary_extension}" "${release_folder}" + mv "target/release/avm${binary_extension}" "${release_folder}" + # strip "${release_folder}/*" + + - name: Publish Artifacts + if: "!startsWith(github.ref, 'refs/tags/')" + uses: actions/upload-artifact@v2 + with: + name: "anchor-${{ steps.build.outputs.release-folder }}-${{ steps.build.set-version.version }}" + path: "${{ steps.build.outputs.release-folder }}/*" + + - name: Release Tags + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + files: | + ${{ steps.build.outputs.release-folder }}/avm* + ${{ steps.build.outputs.release-folder }}/anchor* From daf90cd76af8ca43a28311128a4f13d385f91ac9 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 17:23:16 +0000 Subject: [PATCH 03/18] refactor: trigger on feature branch --- .github/workflows/release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 5e39f45702..5244cfa56e 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -3,7 +3,7 @@ name: Release on: workflow_dispatch: push: - branches: [master] + branches: [master, "feature/windows-support"] tags: - "v*.*.*" From f234935d7ddb714a88c8a2d0a4b4f80ddb0a7aab Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 17:30:39 +0000 Subject: [PATCH 04/18] update artifact action version --- .github/workflows/release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 5244cfa56e..3e010da8eb 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -91,7 +91,7 @@ jobs: - name: Publish Artifacts if: "!startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: "anchor-${{ steps.build.outputs.release-folder }}-${{ steps.build.set-version.version }}" path: "${{ steps.build.outputs.release-folder }}/*" From 72c25c6f9476b06fb5f6f77267223c62f70ff0a7 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 17:55:54 +0000 Subject: [PATCH 05/18] remove hello --- .github/workflows/release.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 3e010da8eb..d998153ef2 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -59,11 +59,11 @@ jobs: run: | # build avm and anchor separately for now because # of the name conflict in avm/anchor and cli/anchor - # cargo build --release -p anchor-cli --bin anchor - # cargo build --release -p avm --bin avm - cargo build --release --bin hello - cp "target/release/hello.exe" "target/release/avm.exe" - cp "target/release/hello.exe" "target/release/anchor.exe" + cargo build --release -p anchor-cli --bin anchor + cargo build --release -p avm --bin avm + # cargo build --release --bin hello + # cp "target/release/hello.exe" "target/release/avm.exe" + # cp "target/release/hello.exe" "target/release/anchor.exe" binary_extension="" From f6bfb11280f7ee1f1c5b237ae01fdbfda191243c Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 18:36:07 +0000 Subject: [PATCH 06/18] Enable build matrix for all os --- .github/workflows/release.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index d998153ef2..958b3a730c 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -13,9 +13,9 @@ jobs: matrix: os: - windows-latest - # - ubuntu-latest - # - macos-latest - # - macos-14 + - ubuntu-latest + - macos-latest + - macos-14 runs-on: ${{ matrix.os }} From fbd9539dc79594f0cf9c905b1526012e43269d80 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 18:36:47 +0000 Subject: [PATCH 07/18] Update to `softprops/action-gh-release@v2` --- .github/workflows/release.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 958b3a730c..61ea9f2f45 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -83,8 +83,8 @@ jobs: fi mkdir -p "${release_folder}" - echo "::set-output name=release-folder::${release_folder}" - + echo "release-folder=${VERSION}" >> "${GITHUB_OUTPUT}" + mv "target/release/anchor${binary_extension}" "${release_folder}" mv "target/release/avm${binary_extension}" "${release_folder}" # strip "${release_folder}/*" @@ -93,11 +93,11 @@ jobs: if: "!startsWith(github.ref, 'refs/tags/')" uses: actions/upload-artifact@v4 with: - name: "anchor-${{ steps.build.outputs.release-folder }}-${{ steps.build.set-version.version }}" + name: "anchor-${{ steps.build.outputs.release-folder }}-${{ steps.set-version.outputs.version }}" path: "${{ steps.build.outputs.release-folder }}/*" - name: Release Tags - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 if: startsWith(github.ref, 'refs/tags/') with: files: | From a499b4012a00849edb83fd3f6dcdcef6822bc4b9 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 18:40:02 +0000 Subject: [PATCH 08/18] Add cache step --- .github/workflows/release.yaml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 61ea9f2f45..2d990db477 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -30,17 +30,17 @@ jobs: default: true override: true - # - name: Cache Cargo registry + index - # uses: actions/cache@v3 - # if: ${{ env.CACHE != 'false' }} - # with: - # path: | - # ~/.cargo/bin/ - # ~/.cargo/registry/index/ - # ~/.cargo/registry/cache/ - # ~/.cargo/git/db/ - # ./target/ - # key: cargo-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} + - name: Cache Cargo registry + index + uses: actions/cache@v3 + if: ${{ env.CACHE != 'false' }} + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + ./target/ + key: cargo-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} - name: Set Version id: set-version From 8526b41697ed8576eb7adb30199de474445a4626 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 18:49:18 +0000 Subject: [PATCH 09/18] Remove `hello` build --- .github/workflows/release.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 2d990db477..101a3c069f 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -61,13 +61,10 @@ jobs: # of the name conflict in avm/anchor and cli/anchor cargo build --release -p anchor-cli --bin anchor cargo build --release -p avm --bin avm - # cargo build --release --bin hello - # cp "target/release/hello.exe" "target/release/avm.exe" - # cp "target/release/hello.exe" "target/release/anchor.exe" binary_extension="" - if [[ "$RUNNER_OS" == "Linux" ]]; then + if [[ "${RUNNER_OS}" == "Linux" ]]; then release_folder="ubuntu-latest" fi if [[ "${RUNNER_OS}" == "Windows" ]]; then From 06913234fb327d5d7c7219fddddfb78451ab1f7a Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 19:16:59 +0000 Subject: [PATCH 10/18] Throw error on unrecognized runner os --- .github/workflows/release.yaml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 101a3c069f..7a05fb37e6 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -66,17 +66,18 @@ jobs: if [[ "${RUNNER_OS}" == "Linux" ]]; then release_folder="ubuntu-latest" - fi - if [[ "${RUNNER_OS}" == "Windows" ]]; then + elif [[ "${RUNNER_OS}" == "Windows" ]]; then binary_extension=".exe" release_folder="windows-latest" - fi - if [[ "${RUNNER_OS}" == "macOS" ]]; then + elif [[ "${RUNNER_OS}" == "macOS" ]]; then if [[ "${{ matrix.os }}" == "macos-14" ]]; then release_folder="macos-m1-latest" else release_folder="macos-intel-latest" fi + else + echo "unknown value for RUNNER_OS: ${RUNNER_OS}" + exit 1 fi mkdir -p "${release_folder}" @@ -84,7 +85,7 @@ jobs: mv "target/release/anchor${binary_extension}" "${release_folder}" mv "target/release/avm${binary_extension}" "${release_folder}" - # strip "${release_folder}/*" + strip "${release_folder}/*" - name: Publish Artifacts if: "!startsWith(github.ref, 'refs/tags/')" From 63c16600374f1975b87e5c3c812ec3fb175fbb21 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 19:17:12 +0000 Subject: [PATCH 11/18] Make buildscript POSIX compatible --- .github/workflows/release.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 7a05fb37e6..2e43123afa 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -64,13 +64,13 @@ jobs: binary_extension="" - if [[ "${RUNNER_OS}" == "Linux" ]]; then + if [ "${RUNNER_OS}" = "Linux" ]; then release_folder="ubuntu-latest" - elif [[ "${RUNNER_OS}" == "Windows" ]]; then + elif [ "${RUNNER_OS}" = "Windows" ]; then binary_extension=".exe" release_folder="windows-latest" - elif [[ "${RUNNER_OS}" == "macOS" ]]; then - if [[ "${{ matrix.os }}" == "macos-14" ]]; then + elif [ "${RUNNER_OS}" = "macOS" ]; then + if [ "${{ matrix.os }}" = "macos-14" ]; then release_folder="macos-m1-latest" else release_folder="macos-intel-latest" @@ -81,11 +81,11 @@ jobs: fi mkdir -p "${release_folder}" - echo "release-folder=${VERSION}" >> "${GITHUB_OUTPUT}" + echo "release-folder=${release_folder}" >> "${GITHUB_OUTPUT}" mv "target/release/anchor${binary_extension}" "${release_folder}" mv "target/release/avm${binary_extension}" "${release_folder}" - strip "${release_folder}/*" + strip "${release_folder}"/* - name: Publish Artifacts if: "!startsWith(github.ref, 'refs/tags/')" From 155e8553843d0ef787a4b1fb0ce1a21ab09eff28 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 21:50:13 +0000 Subject: [PATCH 12/18] Remove `unused mut on variable` warnings on non-windows os --- cli/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 8c0a4eaa0a..20471b47bf 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1524,6 +1524,7 @@ fn build_rust_cwd( no_docs: bool, arch: &ProgramArch, ) -> Result<()> { + #[allow(unused_mut)] let mut cargo_toml_parent = match cargo_toml.parent() { Some(parent) => parent.to_owned(), None => return Err(anyhow!("Unable to find parent")), From 9eeaa029e5af9a965e7efd3bc5ad52a59f69f07f Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Tue, 15 Oct 2024 21:54:51 +0000 Subject: [PATCH 13/18] Switch to `actions-rust-lang/setup-rust-toolchain@v1` to setup rust --- .github/workflows/release.yaml | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 2e43123afa..5c911c6c95 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -23,25 +23,12 @@ jobs: - name: Checkout uses: actions/checkout@v4 - - name: Install Latest Rust - uses: actions-rs/toolchain@v1 + - name: Setup Rust + uses: actions-rust-lang/setup-rust-toolchain@v1 with: toolchain: stable - default: true override: true - - name: Cache Cargo registry + index - uses: actions/cache@v3 - if: ${{ env.CACHE != 'false' }} - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - ./target/ - key: cargo-${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} - - name: Set Version id: set-version shell: sh From d2e0fdc1242eceaf1a7c01e87194a7b092e0dbd5 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Thu, 14 Nov 2024 15:41:35 +0530 Subject: [PATCH 14/18] Add agave PR number for `HOME` env workaround --- cli/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 20471b47bf..292e1b91b6 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1533,7 +1533,7 @@ fn build_rust_cwd( // build-sbf depends on cargo metadata, which in turn depends on glob // to find packages in a workspace // but glob breaks on UNC paths like \\?\C:\...\packages\* - // so on windows, find the relative path of the package and use + // so on windows, use the relative path of the package // https://github.com/rust-lang/glob/issues/132 #[cfg(windows)] { @@ -1932,7 +1932,8 @@ fn _build_rust_cwd( ) -> Result<()> { let mut build_command = std::process::Command::new("cargo"); - // TODO: fix upstream in solana-build-sbf + // TODO: remove once all supported versions include the fix + // https://github.com/anza-xyz/agave/pull/3597 #[cfg(windows)] if std::env::var("HOME").is_err() { build_command.env( From ea8dba58f3698fd6b8b3cdf9081aead64a30a5d9 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Fri, 15 Nov 2024 03:26:58 +0530 Subject: [PATCH 15/18] Fix package manager install error on Windows --- cli/src/config.rs | 13 +++++++++++++ cli/src/lib.rs | 36 +++++++++++++----------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index c8a579f3a2..7342bb5591 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -407,6 +407,19 @@ impl std::fmt::Display for PackageManager { } } +impl PackageManager { + /// On Windows, node executables use the `.cmd` suffix. + /// If not added, `process::Command::new()` will fail to find them. + /// `cmd /c ` is a workaround but it spawns an extra process. + /// This method returns the correct executable name for the current platform. + pub fn executable_name(&self) -> String { + #[cfg(windows)] + return format!("{self}.cmd"); + #[cfg(not(windows))] + return self.to_string(); + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FeaturesConfig { /// Enable account resolution. diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 292e1b91b6..993176340b 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -738,7 +738,7 @@ fn restore_toolchain(restore_cbs: RestoreToolchainCallbacks) -> Result<()> { /// Get the system's default license - what 'npm init' would use. fn get_npm_init_license() -> Result { - let npm_init_license_output = std::process::Command::new("npm") + let npm_init_license_output = std::process::Command::new(PackageManager::NPM.executable_name()) .arg("config") .arg("get") .arg("init-license") @@ -1000,8 +1000,7 @@ fn init( let test_script = test_template.get_test_script(javascript, &package_manager); cfg.scripts.insert("test".to_owned(), test_script); - let package_manager_cmd = package_manager.to_string(); - cfg.toolchain.package_manager = Some(package_manager); + cfg.toolchain.package_manager = Some(package_manager.clone()); let mut localnet = BTreeMap::new(); let program_id = rust_template::get_or_create_program_id(&rust_name); @@ -1077,14 +1076,14 @@ fn init( )?; if !no_install { - let package_manager_result = install_node_modules(&package_manager_cmd)?; + let package_manager_result = install_node_modules(&package_manager)?; - if !package_manager_result.status.success() && package_manager_cmd != "npm" { + if !package_manager_result.status.success() && package_manager != PackageManager::NPM { println!( "Failed {} install will attempt to npm install", - package_manager_cmd + package_manager ); - install_node_modules("npm")?; + install_node_modules(&PackageManager::NPM)?; } else { eprintln!("Failed to install node modules"); } @@ -1107,22 +1106,13 @@ fn init( Ok(()) } -fn install_node_modules(cmd: &str) -> Result { - if cfg!(target_os = "windows") { - std::process::Command::new("cmd") - .arg(format!("/C {cmd} install")) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output() - .map_err(|e| anyhow::format_err!("{} install failed: {}", cmd, e.to_string())) - } else { - std::process::Command::new(cmd) - .arg("install") - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output() - .map_err(|e| anyhow::format_err!("{} install failed: {}", cmd, e.to_string())) - } +fn install_node_modules(cmd: &PackageManager) -> Result { + std::process::Command::new(cmd.executable_name()) + .arg("install") + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(|e| anyhow::format_err!("{} install failed: {}", cmd, e.to_string())) } // Creates a new program crate in the `programs/` directory. From cd12168dea6dac414b1280cad34f89ab76dcb9d6 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Fri, 15 Nov 2024 03:42:37 +0530 Subject: [PATCH 16/18] Fallback to `npm` if selected package manager is not installed --- cli/src/lib.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 993176340b..0257eec09d 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1076,9 +1076,22 @@ fn init( )?; if !no_install { - let package_manager_result = install_node_modules(&package_manager)?; - - if !package_manager_result.status.success() && package_manager != PackageManager::NPM { + let package_manager_result = install_node_modules(&package_manager); + // if the package manager is NOT `npm` and: + // 1. the install command failed to run (eg: binary not found), OR + // 2. the install command ran but didn't finish successfully + // then retry the install with npm + if package_manager != PackageManager::NPM + && package_manager_result.map_or_else( + |err| { + // command failed to run + eprintln!("{}", err); + true + }, + // command ran but didn't finish successfully + |output| !output.status.success(), + ) + { println!( "Failed {} install will attempt to npm install", package_manager From e44c16c560807372423a0f426f49904d01ae2b51 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Fri, 15 Nov 2024 03:46:38 +0530 Subject: [PATCH 17/18] Use `env::var_os()` instead of `env::var()` to prvent unicode issues --- cli/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 0257eec09d..51969eb2bd 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1941,8 +1941,8 @@ fn _build_rust_cwd( if std::env::var("HOME").is_err() { build_command.env( "HOME", - std::env::var("USERPROFILE").map_err(|e| { - anyhow!("env variable 'HOME' not set and could not read 'USERPROFILE': {e}") + std::env::var_os("USERPROFILE").ok_or_else(|| { + anyhow!("env variable 'HOME' not set and could not read 'USERPROFILE'") })?, ); } From 07dc17823edc253a09fc689c3b519faf76bd50ae Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Fri, 15 Nov 2024 03:50:09 +0530 Subject: [PATCH 18/18] Only use `allow(unused_mut)` on `cargo_toml_parent` for `not(windows)` --- cli/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 51969eb2bd..79e5c27e4d 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1527,7 +1527,7 @@ fn build_rust_cwd( no_docs: bool, arch: &ProgramArch, ) -> Result<()> { - #[allow(unused_mut)] + #[cfg_attr(not(windows), allow(unused_mut))] let mut cargo_toml_parent = match cargo_toml.parent() { Some(parent) => parent.to_owned(), None => return Err(anyhow!("Unable to find parent")),