Skip to content

Commit 2863ebc

Browse files
authored
fix: Audit 02/20 (#1838)
* Update aliases. * Add tests. * Fix lints. * Handle git bash. * Fix windows. * Fix.
1 parent 79250fd commit 2863ebc

File tree

8 files changed

+92
-26
lines changed

8 files changed

+92
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
#### 🐞 Fixes
6+
7+
- Fixed an issue where aliases would not be inherited for some toolchains.
8+
- Fixed some token variable interpolation for paths on Windows.
9+
310
## 1.32.5
411

512
#### 🚀 Updates

crates/project-graph/tests/project_graph_test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,18 @@ mod project_graph {
724724
let graph = generate_workspace_graph("expansion").await;
725725
let task = graph.get_task_from_project("tasks", "build").unwrap();
726726

727-
assert_eq!(task.args, string_vec!["a", "../other.yaml", "b"]);
727+
assert_eq!(
728+
task.args,
729+
string_vec![
730+
"a",
731+
if cfg!(windows) {
732+
"..\\other.yaml"
733+
} else {
734+
"../other.yaml"
735+
},
736+
"b"
737+
]
738+
);
728739

729740
assert_eq!(
730741
task.input_files,

crates/task-expander/src/task_expander.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,17 @@ impl<'graph> TaskExpander<'graph> {
161161
"Loading environment variables from .env files",
162162
);
163163

164-
let mut missing_paths = vec![];
165164
let mut merged_env_vars = FxHashMap::default();
166165

167166
// The file may not have been committed, so avoid crashing
168167
for env_path in env_paths {
169168
if env_path.exists() {
169+
trace!(
170+
task_target = task.target.as_str(),
171+
env_file = ?env_path,
172+
"Loading .env file",
173+
);
174+
170175
let handle_error = |error: dotenvy::Error| TasksExpanderError::InvalidEnvFile {
171176
path: env_path.to_path_buf(),
172177
error: Box::new(error),
@@ -179,7 +184,11 @@ impl<'graph> TaskExpander<'graph> {
179184
merged_env_vars.insert(key, val);
180185
}
181186
} else {
182-
missing_paths.push(env_path);
187+
trace!(
188+
task_target = task.target.as_str(),
189+
env_file = ?env_path,
190+
"Skipping .env file because it doesn't exist",
191+
);
183192
}
184193
}
185194

crates/task-expander/src/token_expander.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
1616
use std::borrow::Cow;
1717
use std::env;
1818
use std::mem;
19+
use std::path::Path;
1920
use tracing::{debug, instrument, warn};
2021

2122
#[derive(Debug, Default, PartialEq)]
@@ -580,8 +581,8 @@ impl<'graph> TokenExpander<'graph> {
580581
"arch" => Cow::Borrowed(env::consts::ARCH),
581582
"os" => Cow::Borrowed(env::consts::OS),
582583
"osFamily" => Cow::Borrowed(env::consts::FAMILY),
583-
"workingDir" => Cow::Owned(path::to_string(&self.context.working_dir)?),
584-
"workspaceRoot" => Cow::Owned(path::to_string(&self.context.workspace_root)?),
584+
"workingDir" => Cow::Owned(self.stringify_path(&self.context.working_dir)?),
585+
"workspaceRoot" => Cow::Owned(self.stringify_path(&self.context.workspace_root)?),
585586
// Project
586587
"language" => Cow::Owned(project.language.to_string()),
587588
"project" => Cow::Borrowed(project.id.as_str()),
@@ -592,7 +593,7 @@ impl<'graph> TokenExpander<'graph> {
592593
"projectChannel" => get_metadata(|md| md.channel.as_deref()),
593594
"projectName" => get_metadata(|md| md.name.as_deref()),
594595
"projectOwner" => get_metadata(|md| md.owner.as_deref()),
595-
"projectRoot" => Cow::Owned(path::to_string(&project.root)?),
596+
"projectRoot" => Cow::Owned(self.stringify_path(&project.root)?),
596597
"projectSource" => Cow::Borrowed(project.source.as_str()),
597598
"projectStack" => Cow::Owned(project.stack.to_string()),
598599
"projectType" => Cow::Owned(project.type_of.to_string()),
@@ -738,10 +739,33 @@ impl<'graph> TokenExpander<'graph> {
738739
} else {
739740
let abs_path = path.to_logical_path(&self.context.workspace_root);
740741

741-
path::to_virtual_string(diff_paths(&abs_path, &self.project.root).unwrap_or(abs_path))
742+
self.stringify_path(&diff_paths(&abs_path, &self.project.root).unwrap_or(abs_path))
742743
}
743744
}
744745

746+
fn stringify_path(&self, orig_value: &Path) -> miette::Result<String> {
747+
let value = path::to_string(orig_value)?;
748+
749+
// https://cygwin.com/cygwin-ug-net/cygpath.html
750+
#[cfg(windows)]
751+
if env::var("MSYSTEM").is_ok_and(|value| value == "MINGW32" || value == "MINGW64") {
752+
let mut value = moon_common::path::standardize_separators(value);
753+
754+
if orig_value.is_absolute() {
755+
for drive in 'A'..='Z' {
756+
if let Some(suffix) = value.strip_prefix(&format!("{drive}:/")) {
757+
value = format!("/{}/{suffix}", drive.to_ascii_lowercase());
758+
break;
759+
}
760+
}
761+
}
762+
763+
return Ok(value);
764+
}
765+
766+
Ok(value)
767+
}
768+
745769
fn infer_inputs(&self, task: &mut Task, result: &ExpandedResult) {
746770
if task.options.infer_inputs {
747771
task.input_files.extend(result.files.clone());

crates/task-expander/tests/task_expander_test.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,36 @@ mod task_expander {
245245
env::set_var("FOO_BAR", "foo-bar");
246246

247247
let context = create_context(sandbox.path());
248-
TaskExpander::new(&project, &context)
249-
.expand_args(&mut task)
250-
.unwrap();
248+
let task = TaskExpander::new(&project, &context).expand(&task).unwrap();
251249

252250
env::remove_var("FOO_BAR");
253251
env::remove_var("BAR_BAZ");
254252

255253
assert_eq!(task.args, ["a", "foo-bar", "b", "c/bar-baz/d"]);
256254
}
257255

256+
#[test]
257+
fn replaces_env_vars_from_file() {
258+
let sandbox = create_empty_sandbox();
259+
sandbox.create_file(".env", "FOO_BAR=foo-bar\nBAR_BAZ=bar-baz");
260+
261+
let project = create_project(sandbox.path());
262+
263+
let mut task = create_task();
264+
task.options.env_files = Some(vec![InputPath::WorkspaceFile(".env".into())]);
265+
task.args = vec![
266+
"a".into(),
267+
"$FOO_BAR".into(),
268+
"b".into(),
269+
"c/${BAR_BAZ}/d".into(),
270+
];
271+
272+
let context = create_context(sandbox.path());
273+
let task = TaskExpander::new(&project, &context).expand(&task).unwrap();
274+
275+
assert_eq!(task.args, ["a", "foo-bar", "b", "c/bar-baz/d"]);
276+
}
277+
258278
#[test]
259279
fn replaces_env_var_from_self() {
260280
let sandbox = create_empty_sandbox();
@@ -265,9 +285,7 @@ mod task_expander {
265285
task.env.insert("FOO_BAR".into(), "foo-bar-self".into());
266286

267287
let context = create_context(sandbox.path());
268-
TaskExpander::new(&project, &context)
269-
.expand_args(&mut task)
270-
.unwrap();
288+
let task = TaskExpander::new(&project, &context).expand(&task).unwrap();
271289

272290
assert_eq!(task.args, ["a", "foo-bar-self", "b"]);
273291
}

crates/task-expander/tests/token_expander_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod utils;
22

3-
use moon_common::path::WorkspaceRelativePathBuf;
3+
use moon_common::path::{self, WorkspaceRelativePathBuf};
44
use moon_config::{InputPath, LanguageType, OutputPath, ProjectType};
55
use moon_task_expander::{ExpandedResult, TokenExpander};
66
use rustc_hash::{FxHashMap, FxHashSet};
@@ -277,7 +277,7 @@ mod token_expander {
277277
expander
278278
.replace_variable(&task, Cow::Borrowed("$projectRoot"))
279279
.unwrap(),
280-
project.root.to_string_lossy()
280+
path::to_string(&project.root).unwrap()
281281
);
282282
assert_eq!(
283283
expander
@@ -313,13 +313,13 @@ mod token_expander {
313313
expander
314314
.replace_variable(&task, Cow::Borrowed("$workingDir"))
315315
.unwrap(),
316-
sandbox.path().to_string_lossy()
316+
path::to_string(sandbox.path()).unwrap()
317317
);
318318
assert_eq!(
319319
expander
320320
.replace_variable(&task, Cow::Borrowed("$workspaceRoot"))
321321
.unwrap(),
322-
sandbox.path().to_string_lossy()
322+
path::to_string(sandbox.path()).unwrap()
323323
);
324324

325325
assert!(predicate::str::is_match("[0-9]{4}-[0-9]{2}-[0-9]{2}")

legacy/bun/platform/src/bun_platform.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ impl Platform for BunPlatform {
181181
self.package_names
182182
.insert(package_name.clone(), project_id.to_owned());
183183

184-
aliases_list.push((project_id.to_owned(), package_name.clone()));
184+
if package_name != project_id.as_str() {
185+
aliases_list.push((project_id.to_owned(), package_name.clone()));
186+
}
185187
}
186188
}
187189
}

legacy/node/platform/src/node_platform.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,12 @@ impl Platform for NodePlatform {
181181
PackageJsonCache::read(project_source.to_path(&self.workspace_root))?
182182
{
183183
if let Some(package_name) = package_json.data.name {
184-
debug!(
185-
target: LOG_TARGET,
186-
"Inheriting alias {} for project {}",
187-
color::label(&package_name),
188-
color::id(project_id)
189-
);
190-
191184
self.package_names
192185
.insert(package_name.clone(), project_id.to_owned());
193186

194-
aliases_list.push((project_id.to_owned(), package_name.clone()));
187+
if package_name != project_id.as_str() {
188+
aliases_list.push((project_id.to_owned(), package_name.clone()));
189+
}
195190
}
196191
}
197192
}

0 commit comments

Comments
 (0)