Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/prek/src/languages/bun/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use prek_consts::prepend_paths;
use tracing::debug;

use crate::cli::reporter::HookInstallReporter;
use crate::git::GitCommandExt;
use crate::hook::InstalledHook;
use crate::hook::{Hook, InstallInfo};
use crate::languages::bun::BunRequest;
Expand Down Expand Up @@ -83,6 +84,7 @@ impl LanguageBackend for Bun {
.args(deps)
.env(EnvVars::PATH, new_path)
.env(EnvVars::BUN_INSTALL, &info.env_path)
.isolate_from_git_env()
.check(true)
.output()
.await?;
Expand Down
2 changes: 2 additions & 0 deletions crates/prek/src/languages/dart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use serde::{Deserialize, Serialize};
use tracing::debug;

use crate::cli::reporter::HookInstallReporter;
use crate::git::GitCommandExt;
use crate::hook::{Hook, InstallInfo, InstalledHook};
use crate::hook_entry::PreparedHookEntry;
use crate::languages::{ExecutionEnvironment, LanguageBackend};
Expand Down Expand Up @@ -351,6 +352,7 @@ async fn install_package_config(
.env(EnvVars::PUB_CACHE, env_path)
.arg("pub")
.arg("get")
.isolate_from_git_env()
.check(true)
.output()
.await?;
Expand Down
2 changes: 2 additions & 0 deletions crates/prek/src/languages/haskell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use prek_consts::prepend_paths;
use tracing::debug;

use crate::cli::reporter::HookInstallReporter;
use crate::git::GitCommandExt;
use crate::hook::{Hook, InstallInfo, InstalledHook};
use crate::languages::{ExecutionEnvironment, LanguageBackend};
use crate::process::Cmd;
Expand Down Expand Up @@ -86,6 +87,7 @@ impl LanguageBackend for Haskell {
.arg("--installdir")
.arg(&bin_dir)
.args(pkgs)
.isolate_from_git_env()
.check(true)
.output()
.await
Expand Down
2 changes: 2 additions & 0 deletions crates/prek/src/languages/julia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::debug;

use crate::cli::reporter::HookInstallReporter;
use crate::cli::run::HookRunReporter;
use crate::git::GitCommandExt;
use crate::hook::{Hook, InstallInfo, InstalledHook};
use crate::languages::LanguageBackend;
use crate::process::Cmd;
Expand Down Expand Up @@ -70,6 +71,7 @@ impl LanguageBackend for Julia {
.arg(julia_code)
.arg("--")
.args(&hook.additional_dependencies)
.isolate_from_git_env()
.check(true)
.output()
.await
Expand Down
4 changes: 3 additions & 1 deletion crates/prek/src/languages/node/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tracing::debug;
use url::Url;

use crate::cli::reporter::HookInstallReporter;
use crate::git::GitCommandExt;
use crate::hook::InstalledHook;
use crate::hook::{Hook, InstallInfo};
use crate::languages::node::NodeRequest;
Expand Down Expand Up @@ -341,7 +342,8 @@ impl Npm<'_> {

fn command(&self) -> Cmd {
let mut cmd = Cmd::new(self.executable);
cmd.env(EnvVars::PATH, self.path)
cmd.isolate_from_git_env()
Comment thread
j178 marked this conversation as resolved.
Resolved
.env(EnvVars::PATH, self.path)
.env(EnvVars::NODE_PATH, self.node_path);
for key in NPM_CONFIG_ENVS_TO_REMOVE {
cmd.env_remove(key);
Expand Down
2 changes: 2 additions & 0 deletions crates/prek/src/languages/php.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde_json::{Map, Value, json};
use tracing::debug;

use crate::cli::reporter::HookInstallReporter;
use crate::git::GitCommandExt;
use crate::hook::{Hook, InstallInfo, InstalledHook};
use crate::languages::{ExecutionEnvironment, LanguageBackend};
use crate::process::Cmd;
Expand Down Expand Up @@ -193,6 +194,7 @@ impl LanguageBackend for Php {
.arg("--no-progress")
.arg("--")
.args(dependencies)
.isolate_from_git_env()
.check(true)
.output()
.await
Expand Down
3 changes: 3 additions & 0 deletions crates/prek/src/languages/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use semver::Version;
use tracing::debug;

use crate::cli::reporter::HookInstallReporter;
use crate::git::GitCommandExt;
use crate::hook::{Hook, InstallInfo, InstalledHook};
use crate::languages::{ExecutionEnvironment, LanguageBackend};
use crate::process::Cmd;
Expand Down Expand Up @@ -113,6 +114,7 @@ impl LanguageBackend for Swift {
.arg(repo_path)
.arg("--build-path")
.arg(&build_path)
.isolate_from_git_env()
.check(true)
.output()
.await
Expand All @@ -128,6 +130,7 @@ impl LanguageBackend for Swift {
.arg("--build-path")
.arg(&build_path)
.arg("--show-bin-path")
.isolate_from_git_env()
.check(true)
.output()
.await
Expand Down
82 changes: 81 additions & 1 deletion crates/prek/tests/languages/node.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use assert_cmd::assert::OutputAssertExt;
use assert_fs::assert::PathAssert;
use assert_fs::fixture::{FileWriteStr, PathChild, PathCreateDir};
use prek_consts::PRE_COMMIT_HOOKS_YAML;
use prek_consts::env_vars::{EnvVars, EnvVarsRead};

use crate::common::{TestContext, cmd_snapshot, make_executable, remove_bin_from_path};
use crate::common::{TestContext, cmd_snapshot, git_cmd, make_executable, remove_bin_from_path};

#[test]
fn exec_uses_installed_node_environment() -> anyhow::Result<()> {
Expand Down Expand Up @@ -584,3 +585,82 @@ fn npm_version() {
----- stderr -----
");
}

#[test]
fn node_install_does_not_modify_inherited_git_index() -> anyhow::Result<()> {
let hook_repo = TestContext::new();
hook_repo.init_project();

hook_repo
.work_dir()
.child("package.json")
.write_str(indoc::indoc! {r#"
{
"name": "sentinel-node-tool",
"version": "1.0.0",
"bin": {
"sentinel-node-tool": "cli.js"
}
}
"#})?;
let cli = hook_repo.work_dir().child("cli.js");
cli.write_str(indoc::indoc! {r#"
#!/usr/bin/env node
console.log("sentinel node ok");
"#})?;
make_executable(cli.path())?;
hook_repo
.work_dir()
.child(PRE_COMMIT_HOOKS_YAML)
.write_str(indoc::indoc! {r"
- id: sentinel-node
name: sentinel-node
entry: sentinel-node-tool
language: node
always_run: true
pass_filenames: false
"})?;

hook_repo.git_add(".");
hook_repo.git_commit("Add sentinel Node hook");
hook_repo.git_tag("v1.0.0");

let context = TestContext::new();
context.init_project();
context.write_pre_commit_config(&indoc::formatdoc! {r"
repos:
- repo: {repo}
rev: v1.0.0
hooks:
- id: sentinel-node
", repo = hook_repo.work_dir().display()});
context.git_add(".");

let staged_before = git_cmd(context.work_dir())
.args(["ls-files", "--stage"])
.assert()
.success()
.get_output()
.stdout
.clone();

let git_dir = context.work_dir().child(".git");
context
.run()
.arg("--all-files")
.env(EnvVars::GIT_DIR, git_dir.path())
.env("GIT_INDEX_FILE", git_dir.child("index").path())
.assert()
.success();

let staged_after = git_cmd(context.work_dir())
.args(["ls-files", "--stage"])
.assert()
.success()
.get_output()
.stdout
.clone();
assert_eq!(staged_after, staged_before);

Ok(())
}
Loading