@@ -58,7 +58,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
5858 await this . addNewJdk ( this . currentProjectRoot ) ;
5959 break ;
6060 case "classpath.onWillSelectLibraries" :
61- await this . selectLibraries ( this . currentProjectRoot ) ;
61+ await this . selectLibraries ( this . currentProjectRoot , message . projectType ) ;
6262 break ;
6363 case "classpath.onClickGotoProjectConfiguration" :
6464 this . gotoProjectConfigurationFile ( message . rootUri , message . projectType ) ;
@@ -197,7 +197,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
197197 path : `org.eclipse.jdt.launching.JRE_CONTAINER/${ vmInstallPath } ` ,
198198 } ) ;
199199 }
200- classpathEntries . push ( ...libraries ) ;
200+ classpathEntries . push ( ...libraries . map ( library => this . toClasspathEntryForUpdate ( library , currentProjectRoot ) ) ) ;
201201 if ( classpathEntries . length > 0 ) {
202202 await vscode . commands . executeCommand (
203203 "java.execute.workspaceCommand" ,
@@ -362,7 +362,7 @@ export class ClasspathRequestHandler implements vscode.Disposable {
362362 }
363363 } ) ;
364364
365- private selectLibraries = instrumentOperation ( "projectSettings.classpath.selectLibraries" , async ( _operationId : string , currentProjectRoot : vscode . Uri ) => {
365+ private selectLibraries = instrumentOperation ( "projectSettings.classpath.selectLibraries" , async ( _operationId : string , currentProjectRoot : vscode . Uri , projectType : ProjectType ) => {
366366 const jarFiles : vscode . Uri [ ] | undefined = await vscode . window . showOpenDialog ( {
367367 defaultUri : vscode . workspace . workspaceFolders ?. [ 0 ] . uri ,
368368 openLabel : "Select Jar File" ,
@@ -374,24 +374,42 @@ export class ClasspathRequestHandler implements vscode.Disposable {
374374 } ,
375375 } ) ;
376376 if ( jarFiles ) {
377- const jarPaths : string [ ] = jarFiles . map ( uri => {
378- if ( uri . fsPath . startsWith ( currentProjectRoot . fsPath ) ) {
379- return path . relative ( currentProjectRoot . fsPath , uri . fsPath ) ;
380- }
381- return uri . fsPath ;
382- } ) ;
383377 this . webview . postMessage ( {
384378 command : "classpath.onDidAddLibraries" ,
385- jars : jarPaths . map ( jarPath => {
379+ jars : jarFiles . map ( uri => {
386380 return {
387381 kind : ClasspathEntryKind . Library ,
388- path : jarPath ,
382+ path : this . toLibraryPathForProject ( uri , currentProjectRoot , projectType ) ,
389383 } ;
390384 } ) ,
391385 } ) ;
392386 }
393387 } ) ;
394388
389+ private toLibraryPathForProject ( uri : vscode . Uri , currentProjectRoot : vscode . Uri , projectType : ProjectType ) : string {
390+ if ( projectType !== ProjectType . UnmanagedFolder ) {
391+ return uri . fsPath ;
392+ }
393+
394+ const relativePath = path . relative ( currentProjectRoot . fsPath , uri . fsPath ) ;
395+ if ( relativePath && relativePath !== ".." && ! relativePath . startsWith ( ".." + path . sep ) && ! path . isAbsolute ( relativePath ) ) {
396+ return relativePath ;
397+ }
398+
399+ return uri . fsPath ;
400+ }
401+
402+ private toClasspathEntryForUpdate ( entry : ClasspathEntry , currentProjectRoot : vscode . Uri ) : ClasspathEntry {
403+ if ( entry . kind === ClasspathEntryKind . Library && ! path . isAbsolute ( entry . path ) ) {
404+ return {
405+ ...entry ,
406+ path : path . join ( currentProjectRoot . fsPath , entry . path ) ,
407+ } ;
408+ }
409+
410+ return entry ;
411+ }
412+
395413 private updateUnmanagedFolderLibraries = instrumentOperation ( "projectSettings.classpath.updateUnmanagedFolderLibraries" , async ( _operationId : string , jarFilePaths : string [ ] ) => {
396414 const setting = this . getReferencedLibrariesSetting ( ) ;
397415 setting . include = jarFilePaths ;
0 commit comments