func()

in src/ulsp/internal/workspace-utils/utils.go [60:105]


func (c *workspaceUtilsImpl) GetWorkspaceRoot(ctx context.Context, workspaceFolders []protocol.WorkspaceFolder) (string, error) {
	if len(workspaceFolders) == 0 {
		return "", fmt.Errorf("no workspace folders provided")
	}

	// Find the workspace root and look for any conflicting folders.
	result := ""
	for _, folder := range workspaceFolders {
		// code-workspace files may contain improperly formatted or nonexistent folders.
		// only return an error if no workspace root can be found among any of the given folders.
		fileSystemPath, err := url.Parse(folder.URI)
		if err != nil {
			continue
		}

		out, err := c.fs.WorkspaceRoot(fileSystemPath.Path)
		if err != nil {
			continue
		}

		if result == "" {
			// First result will be used as the workspace root.
			result = string(out)
		} else if result != string(out) {
			// Any difference in subsequent results will be considered a conflict.
			msg := fmt.Sprintf("Workspace root is %q, but a folder in %q is also included. Please remove %q from this workspace and launch it in its own IDE session.", result, string(out), folder.URI)
			c.ideGateway.ShowMessage(ctx, &protocol.ShowMessageParams{
				Type:    protocol.MessageTypeWarning,
				Message: msg,
			})
			c.logger.Warn(msg)
			break
		}
	}

	if result == "" {
		folderStrings := []string{}
		for _, folder := range workspaceFolders {
			folderStrings = append(folderStrings, folder.URI)
		}

		return "", fmt.Errorf("unable to determine a workspace root among the following searched folders: %v", strings.Join(folderStrings, ", "))
	}

	return strings.TrimSpace(string(result)), nil
}