Skip to content

Commit e50e95b

Browse files
authored
Run imported recipes in correct scope (#2835)
1 parent 503511b commit e50e95b

File tree

12 files changed

+66
-33
lines changed

12 files changed

+66
-33
lines changed

src/alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(crate) struct Alias<'src, T = Arc<Recipe<'src>>> {
1414

1515
impl<'src> Alias<'src, Namepath<'src>> {
1616
pub(crate) fn resolve(self, target: Arc<Recipe<'src>>) -> Alias<'src> {
17-
assert!(self.target.last().lexeme() == target.namepath.last().lexeme());
17+
assert!(self.target.last().lexeme() == target.name());
1818

1919
Alias {
2020
attributes: self.attributes,

src/analyzer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
147147

148148
let recipes = RecipeResolver::resolve_recipes(
149149
&assignments,
150+
&ast.module_path,
150151
&self.modules,
151152
&settings,
152153
deduplicated_recipes,

src/compiler.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ impl Compiler {
1616
stack.push(Source::root(root));
1717

1818
while let Some(current) = stack.pop() {
19+
if paths.contains_key(&current.path) {
20+
continue;
21+
}
22+
1923
let (relative, src) = loader.load(root, &current.path)?;
2024
loaded.push(relative.into());
25+
2126
let tokens = Lexer::lex(relative, src)?;
2227
let mut ast = Parser::parse(
2328
current.file_depth,

src/justfile.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ impl<'src> Justfile<'src> {
303303
config: &Config,
304304
dotenv: &BTreeMap<String, String>,
305305
is_dependency: bool,
306-
ran: &Ran<'src>,
306+
ran: &Ran,
307307
recipe: &Recipe<'src>,
308308
scopes: &BTreeMap<String, (&Justfile<'src>, &Scope<'src, '_>)>,
309309
search: &Search,
310310
) -> RunResult<'src> {
311-
let mutex = ran.mutex(&recipe.namepath, arguments);
311+
let mutex = ran.mutex(recipe, arguments);
312312

313313
let mut guard = mutex.lock().unwrap();
314314

@@ -323,7 +323,7 @@ impl<'src> Justfile<'src> {
323323
}
324324

325325
let (module, scope) = scopes
326-
.get(&recipe.module_path())
326+
.get(recipe.module_path())
327327
.expect("failed to retrieve scope for module");
328328

329329
let context = ExecutionContext {
@@ -382,7 +382,7 @@ impl<'src> Justfile<'src> {
382382
dependencies: &[Dependency<'src>],
383383
dotenv: &BTreeMap<String, String>,
384384
evaluator: &mut Evaluator<'src, 'run>,
385-
ran: &Ran<'src>,
385+
ran: &Ran,
386386
recipe: &Recipe<'src>,
387387
scopes: &BTreeMap<String, (&Justfile<'src>, &Scope<'src, 'run>)>,
388388
search: &Search,

src/namepath.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ impl<'src> Namepath<'src> {
88
Self(self.0.iter().copied().chain(iter::once(name)).collect())
99
}
1010

11-
pub(crate) fn spaced(&self) -> ModulePath {
12-
ModulePath {
13-
path: self.0.iter().map(|name| name.lexeme().into()).collect(),
14-
spaced: true,
15-
}
16-
}
17-
1811
pub(crate) fn push(&mut self, name: Name<'src>) {
1912
self.0.push(name);
2013
}

src/parser.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,7 @@ impl<'run, 'src> Parser<'run, 'src> {
10131013
file_depth: self.file_depth,
10141014
import_offsets: self.import_offsets.clone(),
10151015
name,
1016-
namepath: self
1017-
.module_namepath
1018-
.map_or_else(|| name.into(), |module_namepath| module_namepath.join(name)),
1016+
namepath: None,
10191017
parameters: positional.into_iter().chain(variadic).collect(),
10201018
priors,
10211019
private,

src/ran.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use super::*;
22

33
#[derive(Default)]
4-
pub(crate) struct Ran<'src>(
5-
Mutex<BTreeMap<Namepath<'src>, BTreeMap<Vec<String>, Arc<Mutex<bool>>>>>,
6-
);
4+
pub(crate) struct Ran(Mutex<BTreeMap<String, BTreeMap<Vec<String>, Arc<Mutex<bool>>>>>);
75

8-
impl<'src> Ran<'src> {
9-
pub(crate) fn mutex(&self, recipe: &Namepath<'src>, arguments: &[String]) -> Arc<Mutex<bool>> {
6+
impl Ran {
7+
pub(crate) fn mutex(&self, recipe: &Recipe, arguments: &[String]) -> Arc<Mutex<bool>> {
108
self
119
.0
1210
.lock()
1311
.unwrap()
14-
.entry(recipe.clone())
12+
.entry(recipe.namepath().into())
1513
.or_default()
1614
.entry(arguments.into())
1715
.or_default()

src/recipe.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,29 @@ pub(crate) struct Recipe<'src, D = Dependency<'src>> {
2828
#[serde(skip)]
2929
pub(crate) import_offsets: Vec<usize>,
3030
pub(crate) name: Name<'src>,
31-
pub(crate) namepath: Namepath<'src>,
31+
pub(crate) namepath: Option<String>,
3232
pub(crate) parameters: Vec<Parameter<'src>>,
3333
pub(crate) priors: usize,
3434
pub(crate) private: bool,
3535
pub(crate) quiet: bool,
3636
pub(crate) shebang: bool,
3737
}
3838

39+
impl Recipe<'_> {
40+
pub(crate) fn module_path(&self) -> &str {
41+
let namepath = self.namepath();
42+
&namepath[0..namepath.rfind("::").unwrap_or_default()]
43+
}
44+
45+
pub(crate) fn namepath(&self) -> &str {
46+
self.namepath.as_ref().unwrap()
47+
}
48+
49+
pub(crate) fn spaced_namepath(&self) -> String {
50+
self.namepath().replace("::", " ")
51+
}
52+
}
53+
3954
impl<'src, D> Recipe<'src, D> {
4055
pub(crate) fn argument_range(&self) -> RangeInclusive<usize> {
4156
self.min_arguments()..=self.max_arguments()
@@ -57,12 +72,6 @@ impl<'src, D> Recipe<'src, D> {
5772
}
5873
}
5974

60-
pub(crate) fn module_path(&self) -> String {
61-
let mut path = self.namepath.to_string();
62-
path.truncate(path.rfind("::").unwrap_or_default());
63-
path
64-
}
65-
6675
pub(crate) fn name(&self) -> &'src str {
6776
self.name.lexeme()
6877
}

src/recipe_resolver.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use {super::*, CompileErrorKind::*};
22

33
pub(crate) struct RecipeResolver<'src: 'run, 'run> {
44
assignments: &'run Table<'src, Assignment<'src>>,
5+
module_path: &'run str,
56
modules: &'run Table<'src, Justfile<'src>>,
67
resolved_recipes: Table<'src, Arc<Recipe<'src>>>,
78
unresolved_recipes: Table<'src, UnresolvedRecipe<'src>>,
@@ -10,15 +11,17 @@ pub(crate) struct RecipeResolver<'src: 'run, 'run> {
1011
impl<'src: 'run, 'run> RecipeResolver<'src, 'run> {
1112
pub(crate) fn resolve_recipes(
1213
assignments: &'run Table<'src, Assignment<'src>>,
14+
module_path: &'run str,
1315
modules: &'run Table<'src, Justfile<'src>>,
1416
settings: &Settings,
1517
unresolved_recipes: Table<'src, UnresolvedRecipe<'src>>,
1618
) -> CompileResult<'src, Table<'src, Arc<Recipe<'src>>>> {
1719
let mut resolver = Self {
18-
resolved_recipes: Table::new(),
19-
unresolved_recipes,
2020
assignments,
21+
module_path,
2122
modules,
23+
resolved_recipes: Table::new(),
24+
unresolved_recipes,
2225
};
2326

2427
while let Some(unresolved) = resolver.unresolved_recipes.pop() {
@@ -106,7 +109,7 @@ impl<'src: 'run, 'run> RecipeResolver<'src, 'run> {
106109

107110
stack.pop();
108111

109-
let resolved = Arc::new(recipe.resolve(dependencies)?);
112+
let resolved = Arc::new(recipe.resolve(self.module_path, dependencies)?);
110113
self.resolved_recipes.insert(Arc::clone(&resolved));
111114
Ok(resolved)
112115
}

src/subcommand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl Subcommand {
246246

247247
let stdin = child.stdin.as_mut().unwrap();
248248
for recipe in recipes {
249-
if let Err(io_error) = writeln!(stdin, "{}", recipe.namepath.spaced()) {
249+
if let Err(io_error) = writeln!(stdin, "{}", recipe.spaced_namepath()) {
250250
if io_error.kind() != std::io::ErrorKind::BrokenPipe {
251251
return Err(Error::ChooserWrite { io_error, chooser });
252252
}

0 commit comments

Comments
 (0)