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

not able to use exclude functionality #403

Closed
pjangam opened this issue Sep 23, 2016 · 8 comments
Closed

not able to use exclude functionality #403

pjangam opened this issue Sep 23, 2016 · 8 comments

Comments

@pjangam
Copy link

pjangam commented Sep 23, 2016

Expected Behavior

nyc command should work with exclude switch

Observed Behavior

nyc fails to instrument when I use exclude switch in command

Bonus Points! Code (or Repository) that Reproduces Issue

from command line in my project folder
nyc -x submodules/**/*.js --reporter=text --all node_modules/mocha/bin/mocha -- test

Forensic Information

getting error

C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\convert-source-map\index.js:32
    throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
    ^

Error: An error occurred while trying to read the map file at D:\Code\Platform\New\Connector\priceline_net_rate\node_modules\async\dist\async.min.map
Error: ENOENT: no such file or directory, open 'D:\Code\Platform\New\Connector\priceline_net_rate\node_modules\async\dist\async.min.map'
    at readFromFileMap (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\convert-source-map\index.js:32:11)
    at new Converter (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\convert-source-map\index.js:39:32)
    at Object.exports.fromMapFileComment (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\convert-source-map\index.js:112:10)
    at Object.exports.fromMapFileSource (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\convert-source-map\index.js:131:22)
    at NYC._handleSourceMap (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\index.js:279:73)
    at C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\index.js:260:33
    at transform (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\caching-transform\index.js:43:10)
    at C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\node_modules\caching-transform\index.js:51:11
    at NYC._maybeInstrumentSource (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\index.js:249:22)
    at NYC.addFile (C:\Users\pjangam\AppData\Roaming\npm\node_modules\nyc\index.js:135:33)

command run properly when i remove -x submodules/**/*.js

Operating System: windows 8 (64 bit)
Environment Information: output.txt

@bcoe
Copy link
Member

bcoe commented Sep 25, 2016

@pjangam this is definitely a bug, which I've been able to reproduce; there's a workaround you can use in the meantime however.

--exclude no longer includes node_modules by default; so you're walking into your node_modules folder and bumping into a weird issue with async.

I suggest running a command more like this:

nyc -x submodules/**/*.js -x node_modules/ --reporter=text --all ./node_modules/.bin/mocha -- test

Keep in mind that some shells expand globs; so it might be better to set this configuration in a .nycrc or the package.json.

@pjangam
Copy link
Author

pjangam commented Sep 26, 2016

Thanks @bcoe Given command did work for me.

@bcoe bcoe added the bug label Oct 1, 2016
@lxibarra
Copy link

i have vendor.bundle.js file that i want to avoid instrumenting because i get FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
but --exclude or -x is not working for me
my command is like this nyc instrument mybuild/ output/ --exclude mybuild/vendor.bundle.js
nyc seems to ignore the --exclude when instrumenting

@stale
Copy link

stale bot commented Jan 6, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 6, 2019
@JaKXz JaKXz closed this as completed Feb 5, 2019
@knagarajan1984
Copy link

@lxibarra I face the same issue with vendor.js file. can you let me know how you ignored this file?

@lxibarra
Copy link

Hi @knagarajan1984 back then i did not got it to work so i ended up writing a small node script that takes a few parameters to know what folders and/or files to instrument/ignore.
So i run a command like so:

node sh-instrumenter.js --source=build-dev --target=build-instrumentedProd --codefiles=/myapp/source/files --directoryExclusions=/lib,/components,/models,/controllers,/external --fileExclusion=cache_controller.js,vendor.bundle.js,styles.bundle.js,polyfills.bundle.js,routes.config.js,*.js.map

Im not sure if the issue was fixed but if not here is the code for the script, hope it helps. Regards

/*
  This file will instrument your code for tests coverage
  this is an example command you can use
  node sh-instrumenter.js --source=build-dev --target=build-instrumentedProd --codefiles=/myapp/src/scripts --directoryExclusions=/lib,/components,/models,/controllers,/external --fileExclusion=cache_controller.js,vendor.bundle.js,styles.bundle.js,polyfills.bundle.js,routes.config.js,*.js.map
*/

var istanbul = require('istanbul-lib-instrument');
var argv = require('yargs').argv;
var Walker = require('walker');
var fs = require('fs-extra');
var path = require('path');
var source = argv.source;
var target = argv.target;
var codefiles = argv.codefiles;
var _directoryExclusions = (argv.directoryExclusions || '').split(',');
var _fileExclusions = (argv.fileExclusion || '').split(',');
var color_green = '\x1b[32m';
var color_default = '\x1b[0m';
var excludedDirectories = [];
var excludedFiles = [];
var directoryExclusions = _directoryExclusions;

var fileExclusions = _fileExclusions;

function excludeDirectory(directory, stat) {
  var fullPath;
  for (var c = 0; c < directoryExclusions.length; c++) {
    fullPath = target + codefiles + directoryExclusions[c].trim();
    if(fullPath === directory) {
      excludedDirectories.push(directory);
      return false;
    }
  }
  return true;
}

function excludeFile(file) {
  var excluded = fileExclusions.find(function (exclusion) {
     return file.match(exclusion.trim()) === null? false : true;
  }) || false;
  return excluded;
}

function instrumentFile(fileName) {
  var fileSource = fs.readFileSync(fileName, 'utf8');

  var instrumentor = istanbul.createInstrumenter({
    coverageVariable: '___COVERAGE___',
    produceSourceMap: true,
    esModules: true,
  });
  var outputPath = fileName;
  var sourceMapUrl = path.basename(outputPath) + '.map';
  var instrumentedSource = instrumentor.instrumentSync(fileSource, fileName);
  instrumentedSource = instrumentedSource + '\r\n //# sourceMappingURL=' + sourceMapUrl;
  var sourceMap = instrumentor.lastSourceMap();

////# sourceMappingURL=inline.bundle.js.map
  fs.writeFileSync(outputPath, instrumentedSource);
  fs.writeFileSync(outputPath + '.map', JSON.stringify(sourceMap));
}

function copyExcludedElements(excludedArr) {
  excludedArr.forEach(function(elem) {
    console.log(color_default, 'Copying ', elem.replace(target, source), 'to', elem);
    fs.copySync(elem.replace(target, source), elem);
  });
}

fs.removeSync(target);
fs.copySync(source, target);

Walker(target + codefiles)
  .filterDir(excludeDirectory)
  .on('file', function(file, stat) {
    var excluded = excludeFile(file);
    if(excluded === false) {
      console.log('Instrumenting', file);
      instrumentFile(file);
    } else {
      excludedFiles.push(file);
    }
  })
  .on('end', function() {
    console.log(color_green, 'Instrumenting finished');
    console.log(color_green, 'Completing bundle with un instrumented files');
    copyExcludedElements(excludedDirectories);
    copyExcludedElements(excludedFiles);
    console.log(color_green, 'Done');
    console.log(color_default, '.');
  });


`

@coreyfarrell
Copy link
Member

The include and exclude options are not currently supported by the nyc instrument command. This is being worked on at #1007.

@lxibarra
Copy link

The include and exclude options are not currently supported by the nyc instrument command. This is being worked on at #1007.

Good to know thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants