Skip to content

Commit 167d91e

Browse files
authored
Merge pull request #1084 from pkgxdev/fix-conflict-mitigation
`pkgx +yarnpkg.com yarn` works
2 parents 31ba978 + 06403a7 commit 167d91e

2 files changed

Lines changed: 62 additions & 48 deletions

File tree

.github/workflows/ci.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ jobs:
3333
- run: cargo fmt --all -- --check
3434

3535
clippy:
36-
needs: test
3736
strategy:
3837
matrix:
3938
os: [ubuntu-latest, macos-latest]
@@ -61,7 +60,7 @@ jobs:
6160
RUSTFLAGS: "-D warnings"
6261

6362
coverage-unit:
64-
needs: [test, clippy, fmt]
63+
needs: fmt
6564
strategy:
6665
matrix:
6766
os: [ubuntu-latest, macos-latest]
@@ -78,7 +77,7 @@ jobs:
7877
flag-name: ${{ matrix.os }}
7978

8079
coverage-integration:
81-
needs: [test, clippy, fmt]
80+
needs: fmt
8281
strategy:
8382
matrix:
8483
os: [ubuntu-latest, macos-latest]
@@ -92,22 +91,23 @@ jobs:
9291
RUSTFLAGS="-C instrument-coverage" cargo build
9392
echo "$PWD/target/debug" >> $GITHUB_PATH
9493
95-
- name: run integrations
96-
run: |
97-
pkgx --help
98-
pkgx --version
99-
pkgx +git
100-
pkgx +git --json
101-
pkgx +git --json=v1
102-
pkgx git --version
103-
pkgx --silent +git
104-
pkgx --quiet +git
105-
pkgx +git -- git --version # lib/utils.rs:find_program
106-
pkgx --shellcode || true
107-
pkgx -qq git --version
108-
pkgx -s git --version
109-
pkgx -j +git
110-
pkgx /usr/bin/awk --version
94+
- run: pkgx --help
95+
- run: pkgx --version
96+
- run: pkgx +git
97+
- run: pkgx +git --json
98+
- run: pkgx +git --json=v1
99+
- run: pkgx git --version
100+
- run: pkgx --silent +git
101+
- run: pkgx --quiet +git
102+
- run: pkgx +git -- git --version # lib/utils.rs:find_program
103+
- run: pkgx --shellcode || true
104+
- run: pkgx -qq git --version
105+
- run: pkgx -s git --version
106+
- run: pkgx -j +git
107+
- run: pkgx /usr/bin/awk --version
108+
- run: pkgx +yarnpkg.com yarn --version
109+
- run: pkgx +yarnpkg.com -- yarn --version
110+
- run: '! pkgx yarn --version'
111111

112112
- name: generate coverage
113113
run: |

crates/cli/src/main.rs

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ async fn main() -> Result<(), Box<dyn Error>> {
6767

6868
let mut pkgs = vec![];
6969

70+
for pkgspec in plus {
71+
let PackageReq {
72+
project: project_or_cmd,
73+
constraint,
74+
} = PackageReq::parse(&pkgspec)?;
75+
if config
76+
.pantry_dir
77+
.join("projects")
78+
.join(project_or_cmd.clone())
79+
.is_dir()
80+
{
81+
pkgs.push(PackageReq {
82+
project: project_or_cmd,
83+
constraint,
84+
});
85+
} else {
86+
let project = which(&project_or_cmd, &conn, &pkgs).await?;
87+
pkgs.push(PackageReq {
88+
project,
89+
constraint,
90+
});
91+
}
92+
}
93+
7094
if find_program {
7195
let PackageReq {
7296
constraint,
@@ -75,7 +99,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
7599

76100
args[0] = cmd.clone(); // invoke eg. `node` rather than eg. `node@20`
77101

78-
let project = match which(&cmd, &conn).await {
102+
let project = match which(&cmd, &conn, &pkgs).await {
79103
Err(WhichError::CmdNotFound(cmd)) => {
80104
if !did_sync {
81105
if let Some(spinner) = &spinner {
@@ -87,7 +111,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
87111
if let Some(spinner) = &spinner {
88112
spinner.set_message("resolving pkg graph…");
89113
}
90-
which(&cmd, &conn).await
114+
which(&cmd, &conn, &pkgs).await
91115
} else {
92116
Err(WhichError::CmdNotFound(cmd))
93117
}
@@ -102,30 +126,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
102126
});
103127
}
104128

105-
for pkgspec in plus {
106-
let PackageReq {
107-
project: project_or_cmd,
108-
constraint,
109-
} = PackageReq::parse(&pkgspec)?;
110-
if config
111-
.pantry_dir
112-
.join("projects")
113-
.join(project_or_cmd.clone())
114-
.is_dir()
115-
{
116-
pkgs.push(PackageReq {
117-
project: project_or_cmd,
118-
constraint,
119-
});
120-
} else {
121-
let project = which(&project_or_cmd, &conn).await?;
122-
pkgs.push(PackageReq {
123-
project,
124-
constraint,
125-
});
126-
}
127-
}
128-
129129
let companions = pantry_db::companions_for_projects(
130130
&pkgs
131131
.iter()
@@ -262,14 +262,28 @@ impl std::fmt::Display for WhichError {
262262

263263
impl std::error::Error for WhichError {}
264264

265-
async fn which(cmd: &String, conn: &Connection) -> Result<String, WhichError> {
265+
async fn which(cmd: &String, conn: &Connection, pkgs: &[PackageReq]) -> Result<String, WhichError> {
266266
let candidates = pantry_db::which(cmd, conn).map_err(WhichError::DbError)?;
267267
if candidates.len() == 1 {
268268
Ok(candidates[0].clone())
269269
} else if candidates.is_empty() {
270-
return Err(WhichError::CmdNotFound(cmd.clone()));
270+
Err(WhichError::CmdNotFound(cmd.clone()))
271271
} else {
272-
return Err(WhichError::MultipleProjects(cmd.clone(), candidates));
272+
let selected_pkgs = candidates
273+
.clone()
274+
.into_iter()
275+
.filter(|candidate| {
276+
pkgs.iter().any(|pkg| {
277+
let PackageReq { project, .. } = pkg;
278+
project == candidate
279+
})
280+
})
281+
.collect::<Vec<String>>();
282+
if selected_pkgs.len() == 1 {
283+
Ok(selected_pkgs[0].clone())
284+
} else {
285+
Err(WhichError::MultipleProjects(cmd.clone(), candidates))
286+
}
273287
}
274288
}
275289

0 commit comments

Comments
 (0)