Skip to content

OpenBytesWithPrefix leaves a leading slash when the prefix has no trailing separator #49

Description

@andrew

Passing a prefix without a trailing separator produces paths that start with /, which are invalid for io/fs and make NewFS fail.

r, _ := archives.OpenBytesWithPrefix("express-4.19.2.tgz", buf, "package")
fsys, err := archives.NewFS(r)
// err: newfs /LICENSE: invalid argument

"package/" works. The difference comes from stripPrefix in prefix.go, which does a bare strings.TrimPrefix with no separator handling:

stripped.Path = strings.TrimPrefix(f.Path, p.prefix)

ListDir and Extract concatenate the other way (p.prefix + dirPath), so a prefix with a trailing separator is the only form that works consistently, and nothing states that or enforces it.

Normalising the prefix to exactly one trailing separator on construction would make both forms behave the same. A check that the resulting path satisfies fs.ValidPath would turn the remaining cases into an error at OpenBytesWithPrefix rather than a confusing failure later in NewFS.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions