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: .gitignored dirs should be excluded from workspace, pedanticPackages should handle gitignore patterns with leading slash #1222

Merged
merged 7 commits into from
May 29, 2024

Conversation

cakekindel
Copy link
Contributor

@cakekindel cakekindel commented May 11, 2024

Description of the change

Fixes a bug with the globbing changes introduced in 56a36d5 where a .gitignore pattern of /.spago causes spago to (incorrectly) ignore .spago when building the module dependency graph.

This is felt when building with --pedantic-packages, but I imagine it probably causes some other issues, too.

This is caused by some subtle micromatch behavior differences compared with the previous globbing strategy:

--== 56a36d5 src/Spago/Glob.purs L93 ==--
try (SyncFS.readTextFile UTF8 gitignorePath) >>= case _ of
  Left _ -> pure unit
  Right gitignore -> do
    let { ignore, patterns } = gitignoreToMicromatchPatterns base gitignore
    -- when .gitignore is `/.spago`, patterns is `[".spago"]`.
    
    let matcherForThisGitignore = micromatch { ignore } patterns
    let anyIncludePatternWouldBeIgnored = any matcherForThisGitignore includePatterns
    -- when patterns is `[".spago"]`
    -- and includePatterns is `[".spago/p/aff-1.0.0/**/*.purs"]`
    -- then this returns `false` (because `minimatch('foo/**/*')('foo')` returns `false`)

    when (not anyIncludePatternWouldBeIgnored) do
      -- We add all .gitignore patterns to `fsWalk`
      let addMatcher currentMatcher = or [ currentMatcher, matcherForThisGitignore ]
      void $ Ref.modify addMatcher ignoreMatcherRef

Reproduce with

> npm i -g spago@0.93.29
> mkdir gitignore-repro
> cd gitignore-repro
> spago init
> spago build --pedantic-packages
# succeeds
> cat .gitignore | awk '{print "/" $0}' > .gitignore.new; mv .gitignore.new .gitignore
> spago build --pedantic-packages
Sources for package 'gitignore-repro' declares unused dependencies - please remove them from the project config:
  - console
  - effect
  - prelude

These errors can be fixed by running the below command(s):
spago uninstall -p gitignore-repro console effect prelude

Checklist:

  • Added the change to the "Unreleased" section of the changelog
  • Added some example of the new feature to the README
  • Added a test for the contribution (if applicable)

P.S.: the above checks are not compulsory to get a change merged, so you may skip them. However, taking care of them will result in less work for the maintainers and will be much appreciated 😊

@cakekindel
Copy link
Contributor Author

b0bc0de

If this change is contentious for any reason I can revert it - something that's been bothering me about bleeding spago is that it walks gitignored dirs looking for workspace members, including node_modules and errors when there are .gitignored read-protected dirs (ex. a docker container volume)

Copy link
Member

@f-f f-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good - could you add the repro as a test?
It should go in the Pedantic suite, but should use the new copyTree structure from the more recent tests (it's shorter and nicer to work with, we haven't managed to refactor the old tests yet)

@f-f
Copy link
Member

f-f commented May 13, 2024

something that's been bothering me about bleeding spago is that it walks gitignored dirs looking for workspace members, including node_modules and errors when there are .gitignored read-protected dirs (ex. a docker container volume)

But after the latest changes we shouldn't be walking any directories that are being gitignored? Or what am I missing here?

@cakekindel
Copy link
Contributor Author

Sorry! To clarify, the partial gitignore change both significantly improves performance when large node_modules is present and aligns with the behavior I'd expect of not looking for source packages in gitignored dirs.

If scanning gitignored dirs for packages is desired though I can revert that change :)

@Blugatroff
Copy link
Contributor

But after the latest changes we shouldn't be walking any directories that are being gitignored? Or what am I missing here?

I think my previous fix, for not respecting a gitignore in case we are explicitly looking for something which would be ignored (like .spago), was too coarse grained.
Instead of only ignoring the patterns in the gitignore which caused the glob target to be excluded, my change simply ignored the entire .gitignore file along with every pattern inside.
This means, that if your root .gitignore contains the patterns [".spago", "node_modules"] and you glob for something in .spago, then node_modules would also (along with .spago) not be ignored.
On the other hand, this PR creates a distinct micromatch for every pattern in the .gitignore and checks each one of them for a collision with the glob targets separately.

@cakekindel cakekindel changed the title fix: /.spago in .gitignore causes pedanticPackages to spuriously fail fix: spago should not search .gitignored dirs for workspace files, gitignore patterns with leading slash should not fail pedanticPackages May 13, 2024
@cakekindel cakekindel changed the title fix: spago should not search .gitignored dirs for workspace files, gitignore patterns with leading slash should not fail pedanticPackages fix: .gitignored dirs should be excluded from workspace, pedanticPackages should handle gitignore patterns with leading slash May 13, 2024
Copy link
Member

@f-f f-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarifications! This looks great now 🙂

@cakekindel
Copy link
Contributor Author

@f-f Gentle bump on this, I'm not sure why actions thinks the macos task has been running for 16 days when it has clearly finished 😝

@cakekindel
Copy link
Contributor Author

rebased onto master

@f-f
Copy link
Member

f-f commented May 29, 2024

Thanks for the ping! I was waiting for CI to be done then forgot to check on it, sorry 🥲

@f-f f-f merged commit 6c9e1af into purescript:master May 29, 2024
3 checks passed
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.

None yet

3 participants