Skip to content

Commit

Permalink
Merge pull request #539 from snyk/fix/better-debug-output-for-gradle
Browse files Browse the repository at this point in the history
fix: enable debug output from Gradle plugin
  • Loading branch information
kyegupov authored May 29, 2019
2 parents 4a598a5 + c4ab5f6 commit aa4d81a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"snyk-config": "^2.2.1",
"snyk-docker-plugin": "1.25.0",
"snyk-go-plugin": "1.7.2",
"snyk-gradle-plugin": "2.10.4",
"snyk-gradle-plugin": "2.11.1",
"snyk-module": "1.9.1",
"snyk-mvn-plugin": "2.3.0",
"snyk-nodejs-lockfile-parser": "1.13.0",
Expand Down
27 changes: 20 additions & 7 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as abbrev from 'abbrev';

import debugModule = require('debug');

declare interface Global extends NodeJS.Global {
ignoreUnknownCA: boolean;
}
Expand All @@ -10,6 +12,10 @@ const alias = abbrev('copy', 'version', 'debug', 'help', 'quiet', 'interactive',
alias.d = 'debug'; // always make `-d` debug
alias.t = 'test';

// The -d flag enables printing the messages for predefined namespaces.
// Additional ones can be specified (comma-separated) in the DEBUG environment variable.
const DEBUG_DEFAULT_NAMESPACES = ['snyk', 'snyk-gradle-plugin'];

function dashToCamelCase(dash) {
return dash.indexOf('-') < 0
? dash
Expand Down Expand Up @@ -49,20 +55,27 @@ export function args(processargv) {
return acc;
}, {_: []});

// by passing `-d` to the cli, we enable the debugging output, but this must
// be as early as possible in the cli logic to capture all the output
// By passing `-d` to the CLI, we enable the debugging output.
// It needs to happen BEFORE any of the `debug(namespace)` calls needed to create loggers.
// Therefore, the code used by the CLI should create the loggers in a lazy fashion
// or be `require`d after this code.
// TODO(BST-648): sort this out reliably
if (argv.debug) {
let enable = 'snyk';
let enable = DEBUG_DEFAULT_NAMESPACES.join(',');
if (process.env.DEBUG) {
enable += ',' + process.env.DEBUG;
}
require('debug').enable(enable);

// Storing in the global state, because just "debugModule.enable" call won't affect different instances of `debug`
// module imported by plugins, libraries etc.
process.env.DEBUG = enable;

debugModule.enable(enable);
}

const debug = require('debug')('snyk');
const debug = debugModule('snyk');

// this is done after the debug activation line above because we want to see
// the debug messaging when we use the `-d` flag
// Late require, see the note re "debug" option above.
const cli = require('./commands');

// the first argument is the command we'll execute, everything else will be
Expand Down
2 changes: 2 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async function handleError(args, error) {
analytics.add('command', args.command);
} else {
analytics.add('error-message', analyticsError.message);
// Note that error.stack would also contain the error message
// (see https://nodejs.org/api/errors.html#errors_error_stack)
analytics.add('error', analyticsError.stack);
analytics.add('error-code', error.code);
analytics.add('command', args.command);
Expand Down
1 change: 0 additions & 1 deletion src/lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ function postAnalytics(data) {
}

analytics.add = function (key, value) {
debug('analytics adding to metadata: ', key, value);
if (metadata[key]) {
if (!Array.isArray(metadata[key])) {
metadata[key] = [metadata[key]];
Expand Down

0 comments on commit aa4d81a

Please sign in to comment.