Skip to content

Commit

Permalink
sourceaddrs: Use correct canonical form for current directory
Browse files Browse the repository at this point in the history
We previously missed an exception for making the canonical form of a
local source referring to the current directory be "./", to match the
parent directory form "../". This logic was incorrectly requiring that
it be written as "./.", which is silly.

Now "./" and "../" are both accepted as canonical forms, and naked "." and
".." are still rejected as non-canonical as before, which we do just to
avoid injecting further ambiguity into this already-rather-ambiguous
source address syntax so that we'll have more options for potentially
expanding this microsyntax in future.
  • Loading branch information
apparentlymart committed Sep 8, 2023
1 parent 7f973de commit 10490a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sourceaddrs/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ParseSource(given string) (Source, error) {
return nil, fmt.Errorf("a valid source address is required")
}
switch {
case looksLikeLocalSource(given):
case looksLikeLocalSource(given) || given == "." || given == "..":
ret, err := ParseLocalSource(given)
if err != nil {
return nil, fmt.Errorf("invalid local source address %q: %w", given, err)
Expand Down
6 changes: 4 additions & 2 deletions sourceaddrs/source_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ParseLocalSource(given string) (LocalSource, error) {

// We distinguish local source addresses from other address types by them
// starting with some kind of relative path prefix.
if !looksLikeLocalSource(given) {
if !looksLikeLocalSource(given) && given != "." && given != ".." {
return LocalSource{}, fmt.Errorf("must start with either ./ or ../ to indicate a local path")
}

Expand All @@ -55,10 +55,12 @@ func ParseLocalSource(given string) (LocalSource, error) {
// exceptions:
// - we need to retain the leading "./", if it was originally present, to
// disambiguate from module registry addresses.
// - If the cleaned path is just ".." then we need a slash on the end
// - If the cleaned path is just "." or ".." then we need a slash on the end
// because that's part of how we recognize an address as a relative path.
if clean == ".." {
clean = "../"
} else if clean == "." {
clean = "./"
}
if !looksLikeLocalSource(clean) {
clean = "./" + clean
Expand Down
20 changes: 20 additions & 0 deletions sourceaddrs/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ func TestParseSource(t *testing.T) {
relPath: "../boop",
},
},
{
Given: "../",
Want: LocalSource{
relPath: "../",
},
},
{
Given: "..",
WantErr: `invalid local source address "..": relative path must be written in canonical form "../"`,
},
{
Given: "./",
Want: LocalSource{
relPath: "./",
},
},
{
Given: ".",
WantErr: `invalid local source address ".": relative path must be written in canonical form "./"`,
},
{
Given: "../boop/../beep",
WantErr: `invalid local source address "../boop/../beep": relative path must be written in canonical form "../beep"`,
Expand Down

0 comments on commit 10490a2

Please sign in to comment.