Repository navigation
Don't double the separator in Path.absolute at the root - #15
Conversation
When the working directory already ends in a separator (`/` on POSIX, a drive root such as `C:\` on Windows), `absolute` now appends the path to it directly instead of joining with another separator, so `(absolute "foo")` run from `/` gives `/foo` rather than `//foo`. `join` and `</>` are unchanged. The test registers libc's chdir to run `absolute` from `/`, restoring the working directory afterwards.
There was a problem hiding this comment.
Build & Tests
- Head
5b950cc:carp -x test/path.carppasses 134/0 (10-02 compat core, armhf). - Master's
path.carp(5e93015) with this branch'stest/path.carp: 133/1. The one failure is the new root row:Expected value: '(Just @"/foo")', actual value: '(Just @"//foo")'. - CI is green on ubuntu-latest and macos-latest.
What I checked
-
Scope.
cwdis called only fromabsolute(path.carp:198), so no other function can produce this doubled separator.getcwdreturns a path with no trailing separator except the root itself, so on POSIX the new branch fires only at/. By reading:getcwdnever succeeds with an empty string, so(dec (String.length &d))is never -1. -
Probe, built against master and head and run from several working directories:
cwd input master head /foo//foo/foo/a/b//a/b/a/b/""////.//././/usr/usr/usr/tmpfoo/tmp/foo/tmp/foo/tmp""/tmp//tmp/a directory named défoo…/dé/foo…/dé/fooa directory named x\foo…/x\/foo…/x\/fooOnly the rows run from
/change. A multi-byte final character can't match, because UTF-8 continuation bytes are 0x80–0xBF. On POSIX a trailing\is an ordinary byte, so both of those directories still join with a separator. -
Leaks. Built with
--log-memory, 1,000 calls ofabsoluteleaveDebug.memory-balanceunchanged on head and on master. I ran them from/(fooand/usr), from thedédirectory and from/tmp. As a positive control, leaking each result withUnsafe.leakreads 1,000, so the counter works. -
The test helper. No later row depends on the working directory, so the suite wouldn't notice if
absolute-from-rootfailed to restore it. A probe built with the test's own helper prints the samecwdbefore and after the call. -
Mutants, each run against the full suite:
mutant result failing row always String.append(from the body)133/1 absolute prefixes the working directorytest the first byte instead of the last (from the body) 133/1 the same row drop the absolute?branch (from the body)133/1 absolute keeps an absolute path at the root(//usr)read d[len], the NUL, instead of the last byte133/1 absolute does not double the separator at the rootseparator check inverted 132/2 the root row and absolute prefixes the working directoryThe body's three reproduce exactly as it describes. Each arm of the new
if, and the unchangedabsolute?branch, is pinned by its own row.
Findings
None. The change is minimal and does what the body says. Every branch has a row that fails without it, and nothing changes for any working directory other than the root.
The body's other note, (basename "/file") returning "", is a separate bug and isn't part of this PR.
Verdict: merge
A small fix for //foo when run from /, pinned by a row that fails on master, with no change for any other working directory and no leaks.
Path.absolutejoined the working directory and the path withjoin, which always inserts a separator. Run from/(the defaultWORKDIRof a Docker image),(absolute "foo")returned//foo. At a Windows drive root (C:\) it would giveC:\\foo, but that is not tested because there is no Windows CI.absolutenow checks the last byte of the working directory withseparator?. If the directory already ends in a separator, it appends the path directly. Otherwise it joins as before.joinand</>are unchanged, since their explicit join is their own API. Separators are ASCII, so a multi-byte final character can't match.getcwdnever succeeds with an empty string, so the index is never negative.Tests
Core has no way to chdir, so
test/path.carpregisters libc'schdirand adds anabsolute-from-roothelper. It runsabsolutefrom/and then restores the previous working directory. There are three new rows:(absolute "foo")from/is(Just "/foo"). This one fails on master.(absolute "/usr")from/is returned unchanged.(absolute "foo")from the suite's working directory is<cwd>/foo. The existing row only checkedjust?.Master's
path.carp(agit archivecopy) run with the new test file:On this branch all 134 pass. I also broke the fix three ways, and each broken version fails exactly one new row:
<cwd>/foorow (it gives…/mut1foo).absolute?check fails the absolute-input row (it gives//usr).A probe confirmed that the working directory is the same before and after
absolute-from-root. It also showed that an empty path run from/now gives/(master gave//).Other functions
absoluteis the only function that usescwd.relativeworks purely on the strings and handles the root correctly:(relative "/a" "/")is(Just "a")and(relative "/" "/a")is(Just "..").basenamehas a different root bug, not fixed here. It drops the root instead of doubling it:(basename "/file")returns""instead of"/".Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.