Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
feat: introduce javascriptFileExtensions config parameter. (#779)
Browse files Browse the repository at this point in the history
This config parameter can be used to allow users to debug javascript
files that have extensions other than .js.
  • Loading branch information
mctavish authored and DominicKramer committed Nov 8, 2019
1 parent 92a56db commit bf79ce8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/agent/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ export interface ResolvedDebugAgentConfig extends GoogleAuthOptions {
*/
appPathRelativeToRepository?: string;

/**
* The set of file extensions that identify javascript code to be debugged.
* By default, only .js files or files with sourcemaps are considered to be
* debuggable. This setting can be used to inform the debugger if you have
* javascript code in files with extensions other than .js.
* Example: ['.js', '.jsz']
*/
javascriptFileExtensions: string[];

/**
* A function which takes the path of a source file in your repository,
* a list of your project's Javascript files known to the debugger,
Expand Down Expand Up @@ -362,6 +371,7 @@ export const defaultConfig: ResolvedDebugAgentConfig = {
},

appPathRelativeToRepository: undefined,
javascriptFileExtensions: ['.js'],
pathResolver: undefined,
logLevel: 1,
breakpointUpdateIntervalSec: 10,
Expand Down
18 changes: 11 additions & 7 deletions src/agent/debuglet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,23 @@ export class Debuglet extends EventEmitter {
return extend(true, {}, defaultConfig, config, envConfig);
}

static buildRegExp(fileExtensions: string[]): RegExp {
return new RegExp(fileExtensions.map(f => f + '$').join('|'));
}

static async findFiles(
baseDir: string,
config: ResolvedDebugAgentConfig,
precomputedHash?: string
): Promise<FindFilesResult> {
const baseDir = config.workingDirectory;
const fileStats = await scanner.scan(
baseDir,
/.js$|.js.map$/,
Debuglet.buildRegExp(config.javascriptFileExtensions.concat('js.map')),
precomputedHash
);
const jsStats = fileStats.selectStats(/.js$/);
const jsStats = fileStats.selectStats(
Debuglet.buildRegExp(config.javascriptFileExtensions)
);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
const errors = fileStats.errors();
return {jsStats, mapFiles, errors, hash: fileStats.hash};
Expand Down Expand Up @@ -370,10 +377,7 @@ export class Debuglet extends EventEmitter {

let findResults: FindFilesResult;
try {
findResults = await Debuglet.findFiles(
that.config.workingDirectory,
gaeId
);
findResults = await Debuglet.findFiles(that.config, gaeId);
findResults.errors.forEach(that.logger.warn);
} catch (err) {
that.logger.error('Error scanning the filesystem.', err);
Expand Down
3 changes: 2 additions & 1 deletion src/agent/v8/inspector-debugapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export class InspectorDebugApi implements debugapi.DebugApi {
}
const baseScriptPath = path.normalize(breakpoint.location.path);
if (!this.sourcemapper.hasMappingInfo(baseScriptPath)) {
if (!baseScriptPath.endsWith('js')) {
const extension = path.extname(baseScriptPath);
if (!this.config.javascriptFileExtensions.includes(extension)) {
return utils.setErrorStatusAndCallback(
cb,
breakpoint,
Expand Down
3 changes: 2 additions & 1 deletion src/agent/v8/legacy-debugapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export class V8DebugApi implements debugapi.DebugApi {
}
const baseScriptPath = path.normalize(breakpoint.location.path);
if (!this.sourcemapper.hasMappingInfo(baseScriptPath)) {
if (!baseScriptPath.endsWith('.js')) {
const extension = path.extname(baseScriptPath);
if (!this.config.javascriptFileExtensions.includes(extension)) {
return utils.setErrorStatusAndCallback(
cb,
breakpoint,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/hello.jsz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world');
16 changes: 10 additions & 6 deletions test/test-debuglet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,23 @@ describe('Debuglet', () => {
const SOURCEMAP_DIR = path.join(__dirname, 'fixtures', 'sourcemaps');

it('throws an error for an invalid directory', async () => {
const config = extend({}, defaultConfig, {
workingDirectory: path.join(SOURCEMAP_DIR, '!INVALID'),
});
let err: Error | null = null;
try {
await Debuglet.findFiles(
path.join(SOURCEMAP_DIR, '!INVALID!'),
'fake-id'
);
await Debuglet.findFiles(config, 'fake-id');
} catch (e) {
err = e;
}
assert.ok(err);
});

it('finds the correct sourcemaps files', async () => {
const searchResults = await Debuglet.findFiles(SOURCEMAP_DIR, 'fake-id');
const config = extend({}, defaultConfig, {
workingDirectory: SOURCEMAP_DIR,
});
const searchResults = await Debuglet.findFiles(config, 'fake-id');
assert(searchResults.jsStats);
assert.strictEqual(Object.keys(searchResults.jsStats).length, 1);
assert(searchResults.jsStats[path.join(SOURCEMAP_DIR, 'js-file.js')]);
Expand Down Expand Up @@ -717,9 +720,10 @@ describe('Debuglet', () => {
// Don't actually scan the entire filesystem. Act like the filesystem
// is empty.
mockedDebuglet.Debuglet.findFiles = (
baseDir: string,
config: ResolvedDebugAgentConfig,
precomputedHash?: string
): Promise<FindFilesResult> => {
const baseDir = config.workingDirectory;
assert.strictEqual(baseDir, root);
return Promise.resolve({
jsStats: {},
Expand Down
20 changes: 18 additions & 2 deletions test/test-v8debugapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ describe('v8debugapi', () => {
const config: ResolvedDebugAgentConfig = extend({}, defaultConfig, {
workingDirectory: __dirname,
forceNewAgent_: true,
javascriptFileExtensions: ['.js', '.jsz'],
});
const logger = consoleLogLevel({
level: Debuglet.logLevelToName(config.logLevel),
Expand All @@ -253,10 +254,10 @@ describe('v8debugapi', () => {
beforeEach(done => {
if (!api) {
scanner
.scan(config.workingDirectory, /.js$|.js.map$/)
.scan(config.workingDirectory, /.js$|.jsz$|.js.map$/)
.then(async fileStats => {
assert.strictEqual(fileStats.errors().size, 0);
const jsStats = fileStats.selectStats(/.js$/);
const jsStats = fileStats.selectStats(/.js$|.jsz$/);
const mapFiles = fileStats.selectFiles(/.js.map$/, process.cwd());
const mapper = await SourceMapper.create(mapFiles);

Expand Down Expand Up @@ -322,6 +323,21 @@ describe('v8debugapi', () => {
});
});

it('should permit breakpoints on js files with non-standard extensions', done => {
require('./fixtures/hello.jsz');
const bp: stackdriver.Breakpoint = ({
id: 0,
location: {line: 1, path: path.join('fixtures', 'hello.jsz')},
} as {}) as stackdriver.Breakpoint;
api.set(bp, err1 => {
assert.ifError(err1);
api.clear(bp, err2 => {
assert.ifError(err2);
done();
});
});
});

it('should set error for breakpoint in non-js files', done => {
require('./fixtures/key-bad.json');
// TODO(dominickramer): Have this actually implement Breakpoint
Expand Down

0 comments on commit bf79ce8

Please sign in to comment.