From 191ef33468e83b949582dd922e9dbfd7a0318e15 Mon Sep 17 00:00:00 2001 From: WinPlay02 Date: Tue, 19 Dec 2023 21:09:16 +0100 Subject: [PATCH] fix: use correct paths and module names to correctly execute Safe-DS files with spaces (#811) Closes #810 ### Summary of Changes - fix: use correct paths and module names to correctly execute Safe-DS files that contain spaces and fix access to source mappings for these files --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> --- .../generation/safe-ds-python-generator.ts | 6 +++- .../src/extension/pythonServer.ts | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts b/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts index bd369375f..c6a54001f 100644 --- a/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts +++ b/packages/safe-ds-lang/src/language/generation/safe-ds-python-generator.ts @@ -224,7 +224,11 @@ export class SafeDsPythonGenerator { } private formatGeneratedFileName(baseName: string): string { - return `gen_${baseName.replaceAll('%2520', '_').replaceAll(/[ .-]/gu, '_').replaceAll(/\\W/gu, '')}`; + return `gen_${this.sanitizeModuleNameForPython(baseName)}`; + } + + sanitizeModuleNameForPython(moduleName: string): string { + return moduleName.replaceAll('%2520', '_').replaceAll(/[ .-]/gu, '_').replaceAll(/\\W/gu, ''); } private generateModule(module: SdsModule): CompositeGeneratorNode { diff --git a/packages/safe-ds-vscode/src/extension/pythonServer.ts b/packages/safe-ds-vscode/src/extension/pythonServer.ts index 00043be2c..8a658d52a 100644 --- a/packages/safe-ds-vscode/src/extension/pythonServer.ts +++ b/packages/safe-ds-vscode/src/extension/pythonServer.ts @@ -282,11 +282,15 @@ export const executePipeline = async function (services: SafeDsServices, pipelin } mainPipelineName = services.builtins.Annotations.getPythonName(firstPipeline) || firstPipeline.name; if (pipelinePath.endsWith('.sdspipe')) { - mainModuleName = path.basename(pipelinePath, '.sdspipe').replaceAll('-', '_'); + mainModuleName = services.generation.PythonGenerator.sanitizeModuleNameForPython( + path.basename(pipelinePath, '.sdspipe'), + ); } else if (pipelinePath.endsWith('.sdstest')) { - mainModuleName = path.basename(pipelinePath, '.sdstest').replaceAll('-', '_'); + mainModuleName = services.generation.PythonGenerator.sanitizeModuleNameForPython( + path.basename(pipelinePath, '.sdstest'), + ); } else { - mainModuleName = path.basename(pipelinePath).replaceAll('-', '_'); + mainModuleName = services.generation.PythonGenerator.sanitizeModuleNameForPython(path.basename(pipelinePath)); } // const generatedDocuments = services.generation.PythonGenerator.generate(document, { @@ -297,17 +301,23 @@ export const executePipeline = async function (services: SafeDsServices, pipelin let codeMap: ProgramCodeMap = {}; for (const generatedDocument of generatedDocuments) { const fsPath = URI.parse(generatedDocument.uri).fsPath; - lastGeneratedSource.set(fsPath, generatedDocument.getText()); - if (fsPath.endsWith('.map')) { - // exclude sourcemaps - continue; - } + const workspaceRelativeFilePath = path.relative(workspaceRoot, path.dirname(fsPath)); const sdsFileName = path.basename(fsPath); const sdsNoExtFilename = path.extname(sdsFileName).length > 0 ? sdsFileName.substring(0, sdsFileName.length - path.extname(sdsFileName).length) : sdsFileName; - const workspaceRelativeFilePath = path.relative(workspaceRoot, path.dirname(fsPath)); + + lastGeneratedSource.set( + path.join(workspaceRelativeFilePath, sdsFileName).replaceAll('\\', '/'), + generatedDocument.getText(), + ); + // Check for sourcemaps after they are already added to the pipeline context + // This needs to happen after lastGeneratedSource.set, as errors would not get mapped otherwise + if (fsPath.endsWith('.map')) { + // exclude sourcemaps from sending to runner + continue; + } let modulePath = workspaceRelativeFilePath.replaceAll('/', '.').replaceAll('\\', '.'); if (!codeMap.hasOwnProperty(modulePath)) { codeMap[modulePath] = {};