Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Site.copy doing nothing for trailing slashes #426

Merged
merged 1 commit into from
Jun 5, 2023

Conversation

not-my-profile
Copy link
Contributor

Previously

site.copy("static/");

had no effect whatsoever because of the trailing slash.

This commit fixes this bug to recognize the trailing slash and remove it. The trailing slash is still significant since:

site.copy("foo/");

will now only copy a directory "foo" but not a file named "foo" (as you would expect).

Fixes #425.

Previously

    site.copy("static/");

had no effect whatsoever because of the trailing slash.

This commit fixes this bug to recognize the trailing slash and
remove it. The trailing slash is still significant since:

    site.copy("foo/");

will now only copy a directory "foo" but not a file named "foo"
(as you would expect).

Fixes lumeland#425.
@oscarotero
Copy link
Member

Hi! There's indeed a bug here!

Your fix seems to be okay, but I think the real issue is in the normalizePath function that should ensure that a path doesn't end with a slash (unless the path is exactly "/").

This is the way Lume handles internally all paths: always use posix separator (even on Windows), always starts with / and, here is the bug, never should end with / (except the path "/").

Would you like to make these changes?

@not-my-profile
Copy link
Contributor Author

not-my-profile commented Jun 4, 2023

Hi!

I did consider adding the trailing slash trimming to normalizePath, however decided against it since I think it would be counter-intuitive if site.copy("foo/") also copied a file named foo and not just a directory named foo. That is modelling the behavior after the well-known Linux behavior (ENOTDIR), e.g:

  • cp foo/ bar will fail if foo isn't a directory with cp: cannot stat 'foo/': Not a directory.
  • fs.copyFileSync('foo/', 'bar') will fail if foo isn't a directory with Error: ENOTDIR: not a directory, copyfile 'foo/' -> 'bar'.

Hence I introduced the dirOnly to enable the distinction between foo/ and foo. Only that the Site.copy function currently doesn't report any error/warning if the specified file doesn't exist, which I'd however consider to be part of a larger question about error detection, for which I just opened the issue #427.

If we had a mechanism to report non-fatal errors as suggested in #427, we could make site.copy("foo/") report such an error if foo isn't a directory, and thus be consistent with the vast majority of other IO tools / functions. What do you think about this idea?

@oscarotero
Copy link
Member

I see but I think this distinction is no needed because AFAIK, is not possible to have a file and a folder with the same name in the same directory. I think Lume should be able to handle these small details and resolve the correct path without forcing to users to specify it in a specific way. For example, without the normalizePath function, the user would be forced to start all paths with slash, site.copy("foo") wouldn't work, but site.copy("/foo") would.

@not-my-profile
Copy link
Contributor Author

You're right the distinction isn't strictly needed. I just think it would be weird for site.copy("foo/") to also copy a file named foo if it happens to exist since it looks like you have explicitly indicated the source to be a directory and the common practice for that mismatch scenario is to fail. Note that specifying a trailing slash for directories would still be optional, this PR doesn't force you to use trailing slashes for directories ("foo" would still apply to both files and directories).

@oscarotero
Copy link
Member

Okay, good point. Had not thought on that.
Thank!

@oscarotero oscarotero merged commit bae4eac into lumeland:master Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trailing slash breaks Site.copy
2 participants