Skip to content

Commit d6edf88

Browse files
fix: infinite loop when formatting paths (#713)
* fix: use rust 1.85 (#710) * fix: use rust 1.85 * fix: typos failing on typ * fix: codepipeline failures when running cargo (#711) * fix: revert back to rust 1.84 (#712) * fix: infinite loop when formatting paths * remove tracing init from test
1 parent e977cfd commit d6edf88

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

build-config/buildspec-linux-minimal.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ phases:
2020
# Install cargo
2121
- curl --retry 5 --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
2222
- . "$HOME/.cargo/env"
23+
- rustup toolchain install `cat rust-toolchain.toml | grep channel | cut -d '=' -f2 | tr -d ' "'`
2324
# Install cross only if the musl env var is set and not null
2425
- if [ ! -z "${AMAZON_Q_BUILD_MUSL:+x}" ]; then cargo install cross --git https://github.com/cross-rs/cross; fi
2526
# Install python/node via mise (https://mise.jdx.dev/continuous-integration.html)

build-config/buildspec-linux-ubuntu.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ phases:
1919
# Install cargo
2020
- curl --retry 5 --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
2121
- . "$HOME/.cargo/env"
22+
- rustup toolchain install `cat rust-toolchain.toml | grep channel | cut -d '=' -f2 | tr -d ' "'`
2223
# Install tauri-cli, required for building and bundling the desktop app
2324
- cargo install --version 1.6.2 tauri-cli
2425
# Install cross only if the musl env var is set and not null

crates/q_cli/src/cli/chat/tools/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ fn absolute_to_relative(cwd: impl AsRef<Path>, path: impl AsRef<Path>) -> Result
231231
if a == b {
232232
cwd_parts.next();
233233
path_parts.next();
234+
} else {
235+
break;
234236
}
235237
}
236238

@@ -250,6 +252,14 @@ fn absolute_to_relative(cwd: impl AsRef<Path>, path: impl AsRef<Path>) -> Result
250252
fn format_path(cwd: impl AsRef<Path>, path: impl AsRef<Path>) -> String {
251253
absolute_to_relative(cwd, path.as_ref())
252254
.map(|p| p.to_string_lossy().to_string())
255+
// If we have three consecutive ".." then it should probably just stay as an absolute path.
256+
.map(|p| {
257+
if p.starts_with("../../..") {
258+
path.as_ref().to_string_lossy().to_string()
259+
} else {
260+
p
261+
}
262+
})
253263
.unwrap_or(path.as_ref().to_string_lossy().to_string())
254264
}
255265

@@ -407,4 +417,26 @@ mod tests {
407417
"tilde should not expand when not the first component"
408418
);
409419
}
420+
421+
#[tokio::test]
422+
async fn test_format_path() {
423+
async fn assert_paths(cwd: &str, path: &str, expected: &str) {
424+
let ctx = Context::builder().with_test_home().await.unwrap().build_fake();
425+
let fs = ctx.fs();
426+
let cwd = sanitize_path_tool_arg(&ctx, cwd);
427+
let path = sanitize_path_tool_arg(&ctx, path);
428+
fs.create_dir_all(&cwd).await.unwrap();
429+
fs.create_dir_all(&path).await.unwrap();
430+
// Using `contains` since the chroot test directory will prefix the formatted path with a tmpdir
431+
// path.
432+
assert!(format_path(cwd, path).contains(expected));
433+
}
434+
assert_paths("/Users/testuser/src", "/Users/testuser/Downloads", "../Downloads").await;
435+
assert_paths(
436+
"/Users/testuser/projects/MyProject/src",
437+
"/Volumes/projects/MyProject/src",
438+
"/Volumes/projects/MyProject/src",
439+
)
440+
.await;
441+
}
410442
}

typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ zvariant = "zvariant"
3030
ser = "ser"
3131
sur = "sur"
3232
ratatui = "ratatui"
33+
typ = "typ"
3334

3435
[type.rust.extend-identifiers]
3536
# typos really wanted to correct 2ND -> 2AND

0 commit comments

Comments
 (0)