Skip to content

Commit ac261d4

Browse files
authored
fix(date): resolve relative paths in date -r against CWD (#1234)
Closes #1225 — date -r FILE now resolves relative paths against the interpreter's CWD using resolve_path(), matching behavior of other builtins.
1 parent 6d6f2ea commit ac261d4

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

crates/bashkit/src/builtins/date.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
//! caught and return graceful errors.
77
88
use std::fmt::Write;
9-
use std::path::Path;
109

1110
use async_trait::async_trait;
1211
use chrono::format::{Item, StrftimeItems};
1312
use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, TimeZone, Utc};
1413

15-
use super::{Builtin, Context};
14+
use super::{Builtin, Context, resolve_path};
1615
use crate::error::Result;
1716
use crate::interpreter::ExecResult;
1817

@@ -407,8 +406,9 @@ impl Builtin for Date {
407406
let dt_utc;
408407
if let Some(ref file) = ref_file {
409408
// -r / --reference: stat file to get modification time
410-
let path = Path::new(file);
411-
match ctx.fs.stat(path).await {
409+
// Resolve relative paths against CWD (fix for issue #1225)
410+
let path = resolve_path(ctx.cwd, file);
411+
match ctx.fs.stat(&path).await {
412412
Ok(meta) => {
413413
dt_utc = meta.modified.into();
414414
epoch_input = false;

crates/bashkit/tests/spec_cases/bash/date.test.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,23 @@ date +%s --date="Wed, 01 Jan 2020 00:00:00 +0000"
264264
### expect
265265
1577836800
266266
### end
267+
268+
### date_ref_relative_path
269+
# date -r should work with relative paths after cd (issue #1225)
270+
mkdir -p /tmp/datetest
271+
echo "test" > /tmp/datetest/myfile.txt
272+
cd /tmp/datetest
273+
date -r myfile.txt +%Y | grep -qE '^[0-9]{4}$' && echo "valid"
274+
### expect
275+
valid
276+
### end
277+
278+
### date_ref_dot_relative_path
279+
# date -r should work with ./relative paths (issue #1225)
280+
mkdir -p /tmp/datetest2
281+
echo "test" > /tmp/datetest2/file.txt
282+
cd /tmp/datetest2
283+
date -r ./file.txt +%Y | grep -qE '^[0-9]{4}$' && echo "valid"
284+
### expect
285+
valid
286+
### end

0 commit comments

Comments
 (0)