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

feature: add support for ignoring filepaths via micromatch globs #28

Conversation

hydrosquall
Copy link
Contributor

@hydrosquall hydrosquall commented Aug 15, 2021

Motivation

Changes

  • Addresses + documents the feature requests from feature idea: excluded_filenames #21 and excluded_paths is Sort of Not Working #27 with a new excluded_globs option. Note that this can technically be used instead of excluded_paths, but I didn't want to break anyone's existing workflow by modifying an existing option.
  • Added unit tests to test this change + a github action to run the tests
  • Replaced manual string concatenation with path.join() so that this will work on Windows

Testing

  • I was leaning on unit tests, I haven't run this set of changes personally (not sure how to test a GH action locally). @swharden's idea to add a CONTRIBUTING.md is a good one for the long term.
  • Wasn't sure if the built index.js was supposed to get checked into the repo, or gitignored, but recent PRs seem to be updating it so I left it as-is.

Notes

  • I tried minimatch, micromatch, picomatch, and nanomatch. nanomatch was the only one that worked with the leading ./ (minimatch, micromatch), but it was last updated in 2018, is not as performant, and is not as full featured as micromatch.
  • I think micromatch syntax is probably a safe bet given its widespread usage (webpack, babel, eslint, etc).
  • There's not much incentive to be supporting relative paths, since repositories shouldn't be ever trying to access parent directories, I think we'd rather use a more popular globbing library than use
  • During the brief period where nanomatch seemed like a good option, I came up with a TS file: Typecript typings micromatch/nanomatch#21 (comment)

Verified

This commit was signed with the committer’s verified signature.

Verified

This commit was signed with the committer’s verified signature.
@hydrosquall hydrosquall changed the title feature: add support for ignoring glob feature: add support for ignoring filepaths via glob Aug 15, 2021
@hydrosquall hydrosquall force-pushed the cameron.yick/feature/ignore-files-and-folders branch from 960f6fd to 3c64a7e Compare August 15, 2021 00:16
@@ -1,15 +1,18 @@
import fs from "fs";
import * as nodePath from 'path';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Named nodePath to avoid colliding with the existing usage of path as a variable name.

@@ -18,19 +18,33 @@ Default: diagram.svg

## `excluded_paths`

A list of paths to exclude from the diagram, separated by commas.
A list of paths to folders to exclude from the diagram, separated by commas.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically this also worked for paths but only if they were immediate children of root_dir (this is why package.json worked, but src/package.json did not. Rather than explain this nuance, I think it'll be simpler to just only use this config for folders, and nudge people towards mostly using globs instead.

},
"dependencies": {
"@actions/core": "^1.4.0",
"@actions/exec": "^1.1.0",
"d3": "^7.0.0",
"esbuild": "^0.12.15",
"husky": "^7.0.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured it was probably a typo to have 2 versions of the same library.

@@ -0,0 +1,19 @@
{
"compilerOptions": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is trimmed down from the defaults provided by tsc --init, and was included for the benefit of the test-runner.

tsconfig.json Outdated

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was too strict, having all of these on was catching 100s of errors.

@hydrosquall hydrosquall changed the title feature: add support for ignoring filepaths via glob feature: add support for ignoring filepaths via micromatch globs Aug 15, 2021
@@ -22,14 +22,19 @@ const main = async () => {
core.endGroup()


const rootPath = core.getInput("root_path") || "./";
const rootPath = core.getInput("root_path") || ""; // Micro and minimatch do not support paths starting with ./
Copy link
Contributor

Choose a reason for hiding this comment

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

we might want to also remove any leading ./ params, to prevent from breaking existing workflows

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about this - what do you think about a choice between

  1. Quitting the script early if the path starts with ./ and asking the user to remove it
  2. Quietly removing it if it's detected

I think doing 2 automatically is a smoother migration because it introduces a quiet side effect, but 1 is simpler for us to maintain. Otherwise, people may get confused when seeing some configs start with ./ and some without.

@Wattenberger
Copy link
Contributor

this is wonderful @hydrosquall, thanks for digging in here! One question: what's your reasoning behind having excluded_paths and excluded_globs as separate params? Is it possible to have one combined param?

@hydrosquall
Copy link
Contributor Author

this is wonderful @hydrosquall, thanks for digging in here! One question: what's your reasoning behind having excluded_paths and excluded_globs as separate params? Is it possible to have one combined param?

Hi, thank you! I had a few reasons for going with this

  1. Backwards compatibility- it was safer to add a new param with new behavior than to modify an existing param.
  2. excluded_paths is slightly terser/beginner friendly: to express the current filters as node_modules, you'd need to write node_modules/**. It's also slightly more performant to do an excluded_path check (testing set membership vs compiling and running a regular expression), but this is probably a negligible cost for most cases.
  3. excluded_paths matches the behavior of the web app file filters, in case people are copy pasting their exclusion filters from there (as far as I can tell, that's a separate codebase, otherwise I would have updated the matching logic in both spots).

With all of that said, I would be in favor of using the glob param, assuming that

  • it's OK to drop the current param (maybe we leave the old one in as a no-op, and throw an error or warning if people try to set it)
  • our target users are familiar with the idea of what globs are
  • having different filtering params than the web app is OK

const children = [];

for (const fileOrFolder of filesOrFolders) {
const fullPath = nodePath.join(rootPath, fileOrFolder);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think rootPath needs to be path, to include the parents recursively?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right, that was a bug :)

@Wattenberger
Copy link
Contributor

I did some testing and made some small tweaks when running into bugs. It works, but ignores folders - I'm thinking because of the comment above.

FYI you can test the Action by pointing it at any repo@branch:
uses: hydrosquall/[email protected]/feature/ignore-files-and-folders
(eg here https://github.com/Wattenberger/kumiko/blob/master/.github/workflows/diagram.yml#L14)

@hydrosquall
Copy link
Contributor Author

Thanks for fixing the yaml / ./ prefix errors - they look good. I made the adjustment to the fullPath calculation, and am optimistic this should fix it since it restores the previous behavior at

const info = fs.statSync(path + "/" + file);
.

Also, thanks for the tip about how to test github actions - I will use this to try out dev builds going forward.

@Wattenberger
Copy link
Contributor

Wonderful, all looks great! Thanks!

@Wattenberger Wattenberger merged commit 0cd1a63 into githubocto:main Aug 17, 2021
@hydrosquall hydrosquall deleted the cameron.yick/feature/ignore-files-and-folders branch August 20, 2021 03:33
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

2 participants