Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operating system independent home folder variable #141657

Closed
imthenachoman opened this issue Jan 27, 2022 · 6 comments · Fixed by #141902
Closed

operating system independent home folder variable #141657

imthenachoman opened this issue Jan 27, 2022 · 6 comments · Fixed by #141902
Assignees
Labels
feature-request Request for new features or functionality good first issue Issues identified as good for first-time contributors insiders-released Patch has been released in VS Code Insiders on-testplan variable-resolving
Milestone

Comments

@imthenachoman
Copy link

  • On Linux and macOS the user's home folder variable is $HOME.
  • On Windows it is %userprofile%.

This means, if you need to reference something you have to specify it twice: once for Linux/macOS and once for Windows.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "options": {
        "env": {
            "SOME_VAR": "${env:HOME}/path/to/file"
        }
    },
    "windows": {
        "options": {
            "env": {
                "SOME_VAR": "${env:userprofile}\\path\\to\\file",
            }
        }
    }
}

If VS Code had an environment variable (like https://code.visualstudio.com/docs/editor/variables-reference) that pointed to the running OS' home then it would simplify things in tasks.json.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "options": {
        "env": {
            "SOME_VAR": "${userHome}{pathSeparator}path{pathSeparator}to{pathSeparator}file"
        }
    }

Pretty please? With sugar on top?

@connor4312 connor4312 assigned alexr00 and unassigned connor4312 Jan 27, 2022
@alexr00 alexr00 added feature-request Request for new features or functionality variable-resolving labels Jan 28, 2022
@alexr00 alexr00 added this to the On Deck milestone Jan 28, 2022
@alexr00 alexr00 added the good first issue Issues identified as good for first-time contributors label Jan 28, 2022
@Bruce-Hopkins
Copy link
Contributor

Can I work on this issue?

@imthenachoman
Copy link
Author

imthenachoman commented Jan 30, 2022

Happy to test if needed.

Another idea I had, if it is easier or makes more sense, is nested variables processed in order. For example, something like:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "variables": {
        "MY_HOME": "${env:HOME}",
        "windows": {
            "MY_HOME": "${env:userprofile}"
        },
    },
    // then we could use ${MY_HOME} anywhere else....
    // and this would need to be in tasks.json for collaborative development reasons
}

@alexr00
Copy link
Member

alexr00 commented Jan 31, 2022

@Bruce-Hopkins you are welcome to work on it!

First, let's make the first parameter of recursiveResolve here a new type which is IProcessEnvironement & {userHome: URI}.

private recursiveResolve(environment: IProcessEnvironment | undefined, folderUri: uri | undefined, value: any, commandValueMapping?: IStringDictionary<string>, resolvedVariables?: Map<string, string>): any {
if (types.isString(value)) {

Then, you'll need to get the userhome value somewhere around here:

return this.recursiveResolve(await this._envVariablesPromise, workspaceFolder ? workspaceFolder.uri : undefined, result, commandValueMapping, resolvedVariables);
}

For an example of how to get userhome (you won't need the preferLocal options):

protected getUserHome(): Promise<URI> {
return this.pathService.userHome({ preferLocal: this.scheme === Schemas.file });
}

Finally, the variable name will need to be added into this switch:

@Bruce-Hopkins
Copy link
Contributor

@alexr00 thanks! I appreciate the detailed response. If you could help, I have a few questions.

  1. I apologize, but I don't understand what needs to be changed here.

    return this.recursiveResolve(await this._envVariablesPromise, workspaceFolder ? workspaceFolder.uri : undefined, result, commandValueMapping, resolvedVariables);
    }

  2. I'm trying to make a function to get the home path like the example:

    protected getUserHome(): Promise<URI> {
    return this.pathService.userHome({ preferLocal: this.scheme === Schemas.file });
    }

But in the constructor, we don't receive the IPathService interface. Do I add an optional parameter to this constructor or is there a better way?

constructor(_context: IVariableResolveContext, _labelService?: ILabelService, _envVariablesPromise?: Promise<IProcessEnvironment>) {
this._context = _context;

@alexr00
Copy link
Member

alexr00 commented Jan 31, 2022

  1. I apologize, but I don't understand what needs to be changed here.

Once you make the first change ("First, let's make the first parameter of recursiveResolve here a new type which is IProcessEnvironement & {userHome: URI}."), you'll need to update the first parameter on that line.

But in the constructor, we don't receive the IPathService interface. Do I add an optional parameter to this constructor or is there a better way?

Yep, an optional parameter is the way to get IPathService here.

@Bruce-Hopkins
Copy link
Contributor

Is this good? I made the param a union type. Will this prevent needing to make any changes to the envVariablesPromises

https://github.com/Bruce-Hopkins/vscode/blob/35db2798d860e63f2fcb0ddd45bff98b16e8a09d/src/vs/workbench/services/configurationResolver/common/variableResolver.ts#L128-L129

Also, I'm trying to find a way to use pathService, but I don't think I can return the result in the switch statement because the function is asynchronous.

https://github.com/Bruce-Hopkins/vscode/blob/35db2798d860e63f2fcb0ddd45bff98b16e8a09d/src/vs/workbench/services/configurationResolver/common/variableResolver.ts#L193-L196

@alexr00 alexr00 modified the milestones: On Deck, February 2022 Feb 7, 2022
@alexr00 alexr00 added verification-needed Verification of issue is requested and removed verification-needed Verification of issue is requested labels Feb 21, 2022
@alexr00 alexr00 mentioned this issue Feb 21, 2022
4 tasks
@github-actions github-actions bot locked and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality good first issue Issues identified as good for first-time contributors insiders-released Patch has been released in VS Code Insiders on-testplan variable-resolving
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@imthenachoman @connor4312 @alexr00 @Bruce-Hopkins and others