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
feat: Allow negation in --ignore-files #1348
Conversation
We considered the removal of web-ext/src/util/file-filter.js Line 46 in 0fd2dd3
Would that solve the original issue? If not, and this change is necessary, can you include a unit test that fails before this PR and passes with your proposed change? |
@Rob--W looking at the following code: web-ext/src/util/file-filter.js Lines 107 to 113 in bc4eb1c
removing I (very briefly) looked to the @Gozala I agree with @Rob--W that some additional unit tests are needed for this change. |
Nope it would not as intention is to include some libraries installed through npm by manually whitelisting them.
I will update this pull with to include that.
Thanks for pointing that out. I thought I've disabled prettier to avoid that but I guess it still end up changing things. |
e157dc3
to
aba3201
Compare
I have updated this pull request such that:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I only have two minor comments.
src/util/file-filter.js
Outdated
this.filesToIgnore.push(this.resolveWithSourceDir(file)); | ||
if (file.charAt(0) === NEGATION) { | ||
const resolvedFile = this.resolveWithSourceDir(file.substr(1)); | ||
this.filesToIgnore.push(`${NEGATION}${resolvedFile}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the code is as readable if you use !
instead of ${NEGATION}
.
filter.wantFile('/src/node_modules/libdweb/src/lib.js'), | ||
true); | ||
assert.equal( | ||
filter.wantFile('/src/node_modules/libdweb/src/sub/lib.js'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test for /src/node_modules/some/other/node_modules/index.js
(I guess that the path is included because of the negation, but I would not be surprised if it got ignored anyway because of the deeper node_modules/
.
@Rob--W updated pull request to address your comments above. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r+ if tests pass
hmm, I think travis is failing because of pull request title |
- Swaps minimatch for multimatch to allow opt-in inclusion of node_modules subset - Fix test introduced in previous commit - Fix mozilla#1347
@Rob--W Alright it's all green now, as just a pull request title. Can you please merge it in ? Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, given the final goal of this PR (to make web-ext build --ignore-files '!node_modules/somepkg/somepattern'
to include the given file (or files) in the resulting generated zip file), it seems to me that (despite the change applied by this pull request to FileFilter
), zipDir is still going to exclude those files because it calls the 'filter' callback (
Line 128 in 1cd88a1
filter: (...args) => fileFilter.wantFile(...args), |
'/somepath/myproj/node_modules'
doesn't match '!node_modules/somepkg/somepattern'
, zipDir
is not going to descend into that directory.
@Gozala I think that it would be a good idea to add the following additional test case in test.build.js:
it('zips a package and includes a file from a negated filter', () => {
const zipFile = new ZipFile();
return withTempDir(
(tmpDir) =>
build({
sourceDir: fixturePath('minimal-web-ext'),
artifactsDir: tmpDir.path(),
ignoreFiles: [
'!node_modules/pkg1/file1.txt',
],
})
.then((buildResult) => {
assert.match(buildResult.extensionPath,
/minimal_extension-1\.0\.zip$/);
return buildResult.extensionPath;
})
.then((extensionPath) => zipFile.open(extensionPath))
.then(() => zipFile.extractFilenames())
.then((fileNames) => {
fileNames.sort();
assert.deepEqual(fileNames, [
'background-script.js', 'manifest.json',
'node_modules/',
'node_modules/pkg1/',
'node_modules/pkg1/file1.txt',
]);
return zipFile.close();
})
);
});
and to add the following empty files to that fixture extension:
- tests/fixtures/minimal-web-ext/.private-file1.txt
- tests/fixtures/minimal-web-ext/node_modules/pkg1/file1.txt
- tests/fixtures/minimal-web-ext/node_modules/pkg2/file2.txt
This additional files in the fixture would ensure that we still ignore those files by default, because of the existing test case:
web-ext/tests/unit/test-cmd/test.build.js
Lines 31 to 54 in 1cd88a1
it('zips a package', () => { | |
const zipFile = new ZipFile(); | |
return withTempDir( | |
(tmpDir) => | |
build({ | |
sourceDir: fixturePath('minimal-web-ext'), | |
artifactsDir: tmpDir.path(), | |
}) | |
.then((buildResult) => { | |
assert.match(buildResult.extensionPath, | |
/minimal_extension-1\.0\.zip$/); | |
return buildResult.extensionPath; | |
}) | |
.then((extensionPath) => zipFile.open(extensionPath)) | |
.then(() => zipFile.extractFilenames()) | |
.then((fileNames) => { | |
fileNames.sort(); | |
assert.deepEqual(fileNames, | |
['background-script.js', 'manifest.json']); | |
return zipFile.close(); | |
}) | |
); | |
}); |
and the new 'zips a package and includes a file from a negated filter'
test case will confirm if the new additional behaviors related to the negated filters are actually working as expected.
My guess is that with the current implemented changes there are still at least three additional negated patterns that are actually needed to force web-ext build to include that file:
- '!node_modules'
- '!node_modules/pkg1'
- '!node_modules/pkg1/file1.txt'
and so the command line would be:
web-ext build -i '!node_modules/' -i '!node_modules/pkg1/' -i '!node_modules/pkg1/file1.txt'
@rpl Thanks for thorough review, you were right that:
Which is unfortunate, but give how [node-zip-dir](https://github.com/jsantell/node-zip-dir#options works) I am not sure there is a better way. Only other option I can imagine is add negation rules for all parent dirs automatically, so say ignoreFiles is
Given the constraints I think manually negating parent dirs is probably good compromise as it still can achieve goal of including specific directories without introducing too much complexity. Probably best solution would be to change @jsantell/node-zip-dir so that it understands globs itself, but again I think this pull request is probably a good enough compromise. P.S. I have added test you've conveniently provided in the comment, except I modified it slightly to capture how ignore negation should be used to actually include desired package. |
@rpl also it seems that pull did not picked up on requested changes being addressed and I don't seem to see a way to re-trigger another review request, so just posting this comment instead. |
@Gozala Yeah, I definitely agree, I definitely prefer to avoid adding implicitly a bunch of empty directories to workaround that (and yeah, it wouldn't work with more complex negated patterns, which is one more reason to avoid the workaround).
👍 I mainly wanted to be sure that we double-checked the actual behavior and agreed that it would be good enough for now. We will need to document the behavior expected when negated pattern are used in the ignore file params or configs in any case and we can mention this behavior in those additions to the docs (I added the "needs: docs" label on both this PR and the original issue, to make sure that we document this on the MDN doc pages for the web-ext tool).
Sounds good to me too, we can defer that for further follow ups. |
This change swaps minimatch for multimatch to allow opt-in inclusion subset of node_modules
Fix #1347