Skip to content

Commit 27698aa

Browse files
authored
fix: should not error when web build path is not found (#337)
* Skip sourcemaps upload if js files couldnt be found or debug injection fails * Update log * Update CHANGELOG * Fix test
1 parent 0bbfd41 commit 27698aa

File tree

3 files changed

+80
-37
lines changed

3 files changed

+80
-37
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Should not exit program when web build path is not found ([#337](https://github.com/getsentry/sentry-dart-plugin/pull/337))
8+
39
## 3.0.0
410

511
Version 3.0.0 marks a major release of the Sentry Dart Plugin containing breaking changes for Flutter Web.

lib/sentry_dart_plugin.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class SentryDartPlugin {
256256
return sourceMapFiles;
257257
}
258258

259-
Future<void> _injectDebugIds() async {
259+
Future<bool> _injectDebugIds() async {
260260
List<String> params = [];
261261
params.add('sourcemaps');
262262

@@ -265,14 +265,18 @@ class SentryDartPlugin {
265265
// inject each file separately instead of using a directory
266266
// TODO(buenaflor): in the future we should use the directory when sentry-cli is fixed
267267
final jsFilePaths = await _findAllJsFilePaths();
268+
if (jsFilePaths.isEmpty) {
269+
return false;
270+
}
271+
268272
params.add('inject');
269273
for (final path in jsFilePaths) {
270274
params.add(path);
271275
}
272276

273277
params.addAll(_baseCliParams());
274278

275-
await _executeAndLog('Failed to inject debug ids', params);
279+
return await _executeAndLog('Failed to inject debug ids', params);
276280
}
277281

278282
Future<void> _uploadSourceMaps() async {
@@ -455,8 +459,12 @@ class SentryDartPlugin {
455459
const taskName = 'uploading source maps';
456460
Log.startingTask(taskName);
457461

458-
await _injectDebugIds();
459-
await _uploadSourceMaps();
462+
final debugIdInjectionSucceeded = await _injectDebugIds();
463+
if (debugIdInjectionSucceeded) {
464+
await _uploadSourceMaps();
465+
} else {
466+
Log.warn('Skipping source maps upload. Could not inject debug ids.');
467+
}
460468

461469
Log.taskCompleted(taskName);
462470
}
@@ -490,7 +498,7 @@ class SentryDartPlugin {
490498
}
491499
}
492500

493-
Future<void> _executeAndLog(String errorMessage, List<String> params) async {
501+
Future<bool> _executeAndLog(String errorMessage, List<String> params) async {
494502
int? exitCode;
495503

496504
try {
@@ -509,10 +517,11 @@ class SentryDartPlugin {
509517
exitCode = await process.exitCode;
510518
} on Exception catch (exception) {
511519
Log.error('$errorMessage: \n$exception');
520+
return false;
512521
}
513-
if (exitCode != null) {
514-
Log.processExitCode(exitCode);
515-
}
522+
523+
Log.processExitCode(exitCode);
524+
return exitCode == 0;
516525
}
517526

518527
String get _release {

test/plugin_test.dart

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ void main() {
4242
configWriter = ConfigWriter(fs, name);
4343
});
4444

45+
void createJsFilesForTesting() {
46+
// create a temp dir that has the fake js files that we use to inject
47+
// the debug id into
48+
fs.directory('$buildDir/web').createSync(recursive: true);
49+
fs.file('$buildDir/web/file.js').createSync();
50+
fs.file('$buildDir/web/file.js.map').writeAsStringSync('''{
51+
"mappings": "AAAAA,SAASC,cAAc,WAAWC, ...",
52+
"sources": ["../../lib/something.dart", "../lib/foo.dart", "../../../../lib/main.dart", "../../Documents/flutter/packages/flutter/lib/src/widgets/icon_data.dart"],
53+
"sourcesContent": ["document.querySelector('button')"],
54+
"names": ["document","querySelector"],
55+
"version": 3,
56+
"file": "example.min.js.map"
57+
}''');
58+
}
59+
4560
for (final url in const ['http://127.0.0.1', null]) {
4661
for (var fileType in fileTypes) {
4762
group('url: $url', () {
@@ -67,20 +82,11 @@ void main() {
6782
return pm.commandLog.skip(commonCommands.length);
6883
}
6984

70-
test('works with all configuration files', () async {
71-
// create a temp dir that has the fake js files that we use to inject
72-
// the debug id into
73-
fs.directory('$buildDir/web').createSync(recursive: true);
74-
fs.file('$buildDir/web/file.js').createSync();
75-
fs.file('$buildDir/web/file.js.map').writeAsStringSync('''{
76-
"mappings": "AAAAA,SAASC,cAAc,WAAWC, ...",
77-
"sources": ["../../lib/something.dart", "../lib/foo.dart", "../../../../lib/main.dart", "../../Documents/flutter/packages/flutter/lib/src/widgets/icon_data.dart"],
78-
"sourcesContent": ["document.querySelector('button')"],
79-
"names": ["document","querySelector"],
80-
"version": 3,
81-
"file": "example.min.js.map"
82-
}''');
85+
setUp(() {
86+
createJsFilesForTesting();
87+
});
8388

89+
test('works with all configuration files', () async {
8490
const version = '1.0.0';
8591
final config = '''
8692
upload_debug_symbols: true
@@ -103,6 +109,26 @@ void main() {
103109
]);
104110
});
105111

112+
test('ignores sourcemaps upload if no js files found', () async {
113+
const version = '1.0.0';
114+
final config = '''
115+
upload_source_maps: true
116+
web_build_path: not/existing
117+
log_level: debug
118+
ignore_missing: true
119+
''';
120+
final commandLog = await runWith(version, config);
121+
const release = '$name@$version';
122+
123+
final args = '$commonArgs --log-level debug';
124+
expect(commandLog, [
125+
'$cli $args debug-files upload $orgAndProject build/app/outputs',
126+
'$cli $args releases $orgAndProject new $release',
127+
'$cli $args releases $orgAndProject set-commits $release --auto --ignore-missing',
128+
'$cli $args releases $orgAndProject finalize $release'
129+
]);
130+
});
131+
106132
test('fails without args and pubspec', () async {
107133
final exitCode = await plugin.run([]);
108134
expect(exitCode, 1);
@@ -205,8 +231,8 @@ void main() {
205231
final args = commonArgs;
206232
expect(commandLog, [
207233
'$cli $args releases $orgAndProject new $release',
208-
'$cli sourcemaps inject $orgAndProject',
209-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
234+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
235+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
210236
'$cli $args releases $orgAndProject set-commits $release --auto',
211237
'$cli $args releases $orgAndProject finalize $release'
212238
]);
@@ -226,8 +252,8 @@ void main() {
226252
final args = commonArgs;
227253
expect(commandLog, [
228254
'$cli $args releases $orgAndProject new $configRelease',
229-
'$cli sourcemaps inject $orgAndProject',
230-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
255+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
256+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
231257
'$cli $args releases $orgAndProject set-commits $configRelease --auto',
232258
'$cli $args releases $orgAndProject finalize $configRelease'
233259
]);
@@ -249,8 +275,8 @@ void main() {
249275
final args = commonArgs;
250276
expect(commandLog, [
251277
'$cli $args releases $orgAndProject new $release',
252-
'$cli sourcemaps inject $orgAndProject',
253-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
278+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
279+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
254280
'$cli $args releases $orgAndProject set-commits $release --auto',
255281
'$cli $args releases $orgAndProject finalize $release'
256282
]);
@@ -271,8 +297,8 @@ void main() {
271297
final args = commonArgs;
272298
expect(commandLog, [
273299
'$cli $args releases $orgAndProject new $configRelease',
274-
'$cli sourcemaps inject $orgAndProject',
275-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
300+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
301+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
276302
'$cli $args releases $orgAndProject set-commits $configRelease --auto',
277303
'$cli $args releases $orgAndProject finalize $configRelease'
278304
]);
@@ -293,8 +319,8 @@ void main() {
293319
final args = commonArgs;
294320
expect(commandLog, [
295321
'$cli $args releases $orgAndProject new $release',
296-
'$cli sourcemaps inject $orgAndProject',
297-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
322+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
323+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
298324
'$cli $args releases $orgAndProject set-commits $release --auto',
299325
'$cli $args releases $orgAndProject finalize $release'
300326
]);
@@ -318,8 +344,8 @@ void main() {
318344
final args = commonArgs;
319345
expect(commandLog, [
320346
'$cli $args releases $orgAndProject new $release',
321-
'$cli sourcemaps inject $orgAndProject',
322-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
347+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
348+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
323349
'$cli $args releases $orgAndProject set-commits $release --auto',
324350
'$cli $args releases $orgAndProject finalize $release'
325351
]);
@@ -341,8 +367,8 @@ void main() {
341367
final args = commonArgs;
342368
expect(commandLog, [
343369
'$cli $args releases $orgAndProject new $configRelease',
344-
'$cli sourcemaps inject $orgAndProject',
345-
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map $orgAndProject',
370+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
371+
'$cli $args sourcemaps upload $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
346372
'$cli $args releases $orgAndProject set-commits $configRelease --auto',
347373
'$cli $args releases $orgAndProject finalize $configRelease'
348374
]);
@@ -351,6 +377,8 @@ void main() {
351377
test(
352378
'used from config but not replacing build/dist in config release',
353379
() async {
380+
createJsFilesForTesting();
381+
354382
const version = '1.0.0';
355383
final configRelease = 'fixture-configRelease+configDist';
356384
final configDist = 'configDist';
@@ -367,8 +395,8 @@ void main() {
367395
final args = commonArgs;
368396
expect(commandLog, [
369397
'$cli $args releases $orgAndProject new $configRelease',
370-
'$cli sourcemaps inject $orgAndProject',
371-
'$cli $args sourcemaps upload --url-prefix ~/app/ $buildDir/web --ext js --ext map $orgAndProject',
398+
'$cli sourcemaps inject build/web/file.js $orgAndProject',
399+
'$cli $args sourcemaps upload --url-prefix ~/app/ $buildDir/web --ext js --ext map --strip-prefix ../../Documents --strip-prefix ../../../../ --strip-prefix ../../ --strip-prefix ../ $orgAndProject',
372400
'$cli $args releases $orgAndProject set-commits $configRelease --auto',
373401
'$cli $args releases $orgAndProject finalize $configRelease'
374402
]);

0 commit comments

Comments
 (0)