@@ -8,7 +8,7 @@ import { localize } from 'vs/nls';
88import { IWorkspaceFolder , IWorkspace } from 'vs/platform/workspace/common/workspace' ;
99import { URI , UriComponents } from 'vs/base/common/uri' ;
1010import { isWindows , isLinux , isMacintosh } from 'vs/base/common/platform' ;
11- import { extname } from 'vs/base/common/path' ;
11+ import { extname , isAbsolute } from 'vs/base/common/path' ;
1212import { dirname , resolvePath , isEqualAuthority , isEqualOrParent , relativePath , extname as resourceExtname } from 'vs/base/common/resources' ;
1313import * as jsonEdit from 'vs/base/common/jsonEdit' ;
1414import * as json from 'vs/base/common/json' ;
@@ -206,21 +206,22 @@ const SLASH = '/';
206206 * Undefined is returned if the folderURI and the targetConfigFolderURI don't have the same schema or authority
207207 *
208208 * @param folderURI a workspace folder
209+ * @param forceAbsolute if set, keep the path absolute
209210 * @param folderName a workspace name
210211 * @param targetConfigFolderURI the folder where the workspace is living in
211212 * @param useSlashForPath if set, use forward slashes for file paths on windows
212213 */
213- export function getStoredWorkspaceFolder ( folderURI : URI , folderName : string | undefined , targetConfigFolderURI : URI , useSlashForPath = ! isWindows ) : IStoredWorkspaceFolder {
214+ export function getStoredWorkspaceFolder ( folderURI : URI , forceAbsolute : boolean , folderName : string | undefined , targetConfigFolderURI : URI , useSlashForPath = ! isWindows ) : IStoredWorkspaceFolder {
214215
215216 if ( folderURI . scheme !== targetConfigFolderURI . scheme ) {
216217 return { name : folderName , uri : folderURI . toString ( true ) } ;
217218 }
218219
219- let folderPath : string | undefined ;
220- if ( isEqualOrParent ( folderURI , targetConfigFolderURI ) ) {
221- // use relative path
222- folderPath = relativePath ( targetConfigFolderURI , folderURI ) || '.' ; // always uses forward slashes
223- if ( isWindows && folderURI . scheme === Schemas . file && ! useSlashForPath ) {
220+ let folderPath = ! forceAbsolute ? relativePath ( targetConfigFolderURI , folderURI ) : undefined ;
221+ if ( folderPath !== undefined ) {
222+ if ( folderPath . length === 0 ) {
223+ folderPath = '.' ;
224+ } else if ( isWindows && folderURI . scheme === Schemas . file && ! useSlashForPath ) {
224225 // Windows gets special treatment:
225226 // - use backslahes unless slash is used by other existing folders
226227 folderPath = folderPath . replace ( / \/ / g, '\\' ) ;
@@ -252,7 +253,7 @@ export function getStoredWorkspaceFolder(folderURI: URI, folderName: string | un
252253 * Rewrites the content of a workspace file to be saved at a new location.
253254 * Throws an exception if file is not a valid workspace file
254255 */
255- export function rewriteWorkspaceFileForNewLocation ( rawWorkspaceContents : string , configPathURI : URI , targetConfigPathURI : URI ) {
256+ export function rewriteWorkspaceFileForNewLocation ( rawWorkspaceContents : string , configPathURI : URI , isFromUntitledWorkspace : boolean , targetConfigPathURI : URI ) {
256257 let storedWorkspace = doParseStoredWorkspace ( configPathURI , rawWorkspaceContents ) ;
257258
258259 const sourceConfigFolder = dirname ( configPathURI ) ;
@@ -261,12 +262,17 @@ export function rewriteWorkspaceFileForNewLocation(rawWorkspaceContents: string,
261262 const rewrittenFolders : IStoredWorkspaceFolder [ ] = [ ] ;
262263 const slashForPath = useSlashForPath ( storedWorkspace . folders ) ;
263264
264- // Rewrite absolute paths to relative paths if the target workspace folder
265- // is a parent of the location of the workspace file itself. Otherwise keep
266- // using absolute paths.
267265 for ( const folder of storedWorkspace . folders ) {
268- let folderURI = isRawFileWorkspaceFolder ( folder ) ? resolvePath ( sourceConfigFolder , folder . path ) : URI . parse ( folder . uri ) ;
269- rewrittenFolders . push ( getStoredWorkspaceFolder ( folderURI , folder . name , targetConfigFolder , slashForPath ) ) ;
266+ const folderURI = isRawFileWorkspaceFolder ( folder ) ? resolvePath ( sourceConfigFolder , folder . path ) : URI . parse ( folder . uri ) ;
267+ let absolute ;
268+ if ( isFromUntitledWorkspace ) {
269+ // if it was an untitled workspace, try to make paths relative
270+ absolute = false ;
271+ } else {
272+ // for existing workspaces, preserve whether a path was absolute or relative
273+ absolute = ! isRawFileWorkspaceFolder ( folder ) || isAbsolute ( folder . path ) ;
274+ }
275+ rewrittenFolders . push ( getStoredWorkspaceFolder ( folderURI , absolute , folder . name , targetConfigFolder , slashForPath ) ) ;
270276 }
271277
272278 // Preserve as much of the existing workspace as possible by using jsonEdit
0 commit comments