From 350c04b0bc28bc57acb12d65c6b12cff1777ccea Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Thu, 14 Jul 2022 14:44:57 +0200 Subject: [PATCH] Fix the incorrect error message from the CLI. Signed-off-by: Akos Kitta --- .../src/node/sketches-service-impl.ts | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/arduino-ide-extension/src/node/sketches-service-impl.ts b/arduino-ide-extension/src/node/sketches-service-impl.ts index 288641f16..f346f5261 100644 --- a/arduino-ide-extension/src/node/sketches-service-impl.ts +++ b/arduino-ide-extension/src/node/sketches-service-impl.ts @@ -10,7 +10,7 @@ import { promisify } from 'util'; import URI from '@theia/core/lib/common/uri'; import { FileUri } from '@theia/core/lib/node'; import { isWindows, isOSX } from '@theia/core/lib/common/os'; -import { ConfigService } from '../common/protocol/config-service'; +import { ConfigServiceImpl } from './config-service-impl'; import { SketchesService, Sketch, @@ -50,8 +50,8 @@ export class SketchesServiceImpl ? tempDir : maybeNormalizeDrive(fs.realpathSync.native(tempDir)); - @inject(ConfigService) - protected readonly configService: ConfigService; + @inject(ConfigServiceImpl) + protected readonly configService: ConfigServiceImpl; @inject(NotificationServiceServerImpl) protected readonly notificationService: NotificationServiceServerImpl; @@ -205,7 +205,14 @@ export class SketchesServiceImpl if (err) { reject( isNotFoundError(err) - ? SketchesError.NotFound(err.details, uri) + ? SketchesError.NotFound( + fixErrorMessage( + err, + requestSketchPath, + this.configService.cliConfiguration?.directories.user + ), + uri + ) : err ); return; @@ -583,6 +590,36 @@ interface SketchWithDetails extends Sketch { readonly mtimeMs: number; } +// https://github.com/arduino/arduino-cli/issues/1797 +function fixErrorMessage( + err: ServiceError, + sketchPath: string, + sketchbookPath: string | undefined +): string { + if (!sketchbookPath) { + return err.details; // No way to repair the error message. The current sketchbook path is not available. + } + // Original: `Can't open sketch: no valid sketch found in /Users/a.kitta/Documents/Arduino: missing /Users/a.kitta/Documents/Arduino/Arduino.ino` + // Fixed: `Can't open sketch: no valid sketch found in /Users/a.kitta/Documents/Arduino: missing $sketchPath` + const message = err.details; + const incorrectMessageSuffix = path.join(sketchbookPath, 'Arduino.ino'); + if ( + message.startsWith("Can't open sketch: no valid sketch found in") && + message.endsWith(`${incorrectMessageSuffix}`) + ) { + const sketchName = path.basename(sketchPath); + const correctMessagePrefix = message.substring( + 0, + message.length - incorrectMessageSuffix.length + ); + return `${correctMessagePrefix}${path.join( + sketchPath, + `${sketchName}.ino` + )}`; + } + return err.details; +} + function isNotFoundError(err: unknown): err is ServiceError { return ServiceError.is(err) && err.code === 5; // `NOT_FOUND` https://grpc.github.io/grpc/core/md_doc_statuscodes.html }