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 pack includes files from node_modules, closes #2047 #2063

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions __tests__/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export async function run(

const reporter = new reporters.ConsoleReporter({stdout, stderr: stdout});

const cwd = path.join(
const tmpRoot = path.join(
os.tmpdir(),
`yarn-${path.basename(dir)}-${Math.random()}`,
);
await fs.unlink(cwd);

await fs.unlink(tmpRoot);
const cwd = path.join(tmpRoot, 'cwd');
await fs.copy(dir, cwd, reporter);

for (const {basename, absolute} of await fs.walk(cwd)) {
Expand All @@ -63,20 +65,15 @@ export async function run(
const config = new Config(reporter);
await config.init({
cwd,
globalFolder: path.join(cwd, '.yarn/.global'),
cacheFolder: path.join(cwd, '.yarn'),
linkFolder: path.join(cwd, '.yarn/.link'),
globalFolder: path.join(tmpRoot, '.yarn/.global'),
cacheFolder: path.join(tmpRoot, '.yarn'),
linkFolder: path.join(tmpRoot, '.yarn/.link'),
});

await pack(config, reporter, flags, []);

try {
if (checkArchive) {
await checkArchive(config);
}
} finally {
// clean up
await fs.unlink(cwd);
if (checkArchive) {
await checkArchive(config);
}
} catch (err) {
throw new Error(`${err && err.stack} \nConsole output:\n ${out}`);
Expand All @@ -86,22 +83,17 @@ export async function run(
export async function getFilesFromArchive(source, destination): Promise<Array<string>> {
const unzip = new Promise((resolve, reject) => {
fs2.createReadStream(source)
.pipe(zlib.createUnzip())
.pipe(new zlib.Gunzip())
.pipe(tar.Extract({path: destination, strip: 1}))
.on('end', () => {
resolve();
})
.on('error', (error) => {
reject(error);
});
.on('end', resolve)
.on('error', reject);
});
await unzip;
const files = await fs.readdir(destination);
return files;
}

// skip for now because this test fails on travis
test.concurrent.skip('pack should work with a minimal example', (): Promise<void> => {
test.concurrent('pack should work with a minimal example', (): Promise<void> => {
return runPack({}, 'minimal', async (config): Promise<void> => {
const {cwd} = config;
const files = await getFilesFromArchive(
Expand Down Expand Up @@ -142,6 +134,19 @@ test.concurrent('pack should include mandatory files not listed in files array i
});
});

test.concurrent('pack should exclude mandatory files from ignored directories', (): Promise<void> => {
return runPack({}, 'exclude-mandatory-files-from-ignored-directories', async (config): Promise<void> => {
const {cwd} = config;
const files = await getFilesFromArchive(
path.join(cwd, 'exclude-mandatory-files-from-ignored-directories-v1.0.0.tgz'),
path.join(cwd, 'exclude-mandatory-files-from-ignored-directories-v1.0.0'),
);
assert(files.indexOf('index.js') >= 0);
assert(files.indexOf('package.json') >= 0);
assert(files.indexOf('node_modules') === -1);
});
});

test.concurrent('pack should exclude all other files if files array is not empty',
(): Promise<void> => {
return runPack({}, 'files-exclude', async (config): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
modules.exports = function (x) {
return x;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "exclude-mandatory-files-from-ignored-directories",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
21 changes: 13 additions & 8 deletions src/cli/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ const IGNORE_FILENAMES = [
'.gitignore',
];

const DEFAULT_IGNORE = ignoreLinesToRegex([
const FOLDERS_IGNORE = [
// never allow version control folders
'.git',
'CVS',
'.svn',
'.hg',

'node_modules',
];

const DEFAULT_IGNORE = ignoreLinesToRegex([
...FOLDERS_IGNORE,

// ignore cruft
'yarn.lock',
'.lock-wscript',
Expand All @@ -38,15 +44,14 @@ const DEFAULT_IGNORE = ignoreLinesToRegex([
'.npmignore',
'.gitignore',
'.DS_Store',
'node_modules',
]);

const NEVER_IGNORE = ignoreLinesToRegex([
// never ignore these files
'!package.json',
'!readme*',
'!+(license|licence)*',
'!+(changes|changelog|history)*',
'!/package.json',
'!/readme*',
'!/+(license|licence)*',
'!/+(changes|changelog|history)*',
]);

function addEntry(packer: any, entry: Object, buffer?: ?Buffer): Promise<void> {
Expand Down Expand Up @@ -85,7 +90,7 @@ export async function pack(config: Config, dir: string): Promise<stream$Duplex>
if (onlyFiles) {
let lines = [
'*', // ignore all files except those that are explicitly included with a negation filter
'.*', // files with "." as first character have to be excluded explicitly
'.*', // files with "." as first character have to be excluded explicitly
];
lines = lines.concat(
onlyFiles.map((filename: string): string => `!${filename}`),
Expand All @@ -95,7 +100,7 @@ export async function pack(config: Config, dir: string): Promise<stream$Duplex>
}

//
const files = await fs.walk(config.cwd);
const files = await fs.walk(config.cwd, null, new Set(FOLDERS_IGNORE));

// create ignores
for (const file of files) {
Expand Down