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

Add isWsl flag to session/action objects #3046

Closed
wants to merge 7 commits into from
Closed

Add isWsl flag to session/action objects #3046

wants to merge 7 commits into from

Conversation

rfgamaral
Copy link

@rfgamaral rfgamaral commented May 22, 2018

The isWsl flag denotes if the running shell is a Windows Subsystem for Linux (WSL) shell by pattern matching the user configurable shell property.

This flag has support for invoking WSL with multiple executables:

  • bash.exe
  • debian.exe
  • kali.exe
  • opensuse.exe
  • ubuntu.exe
  • wsl.exe

I'm not sure if this is something you guys want to add to Hyper core but it's just a small flag to help plugin developers easily know if the running shell is WSL or not, just like they currently need to know if the host OS is Windows and Linux (provided by process.platform) to properly do their plugin magic. This will come in handy to better support WSL within plugins.

Let me know if you have any questions or a better idea to implement this.

The `isWsl` flag denotes if the running shell is a Windows Subsystem for
Linux (WSL) shell by pattern matching the user configurable `shell`
property.

This flag has support for invoking WSL with multiple executables:

- bash.exe
- debian.exe
- kali.exe
- opensuse.exe
- ubuntu.exe
- wsl.exe
app/ui/window.js Outdated
@@ -93,6 +93,7 @@ module.exports = class Window {
cwd: process.argv[1] && isAbsolute(process.argv[1]) ? process.argv[1] : cfgDir,
splitDirection: undefined,
shell: cfg.shell,
isWsl: /(?:bash|debian|kali|opensuse|ubuntu|wsl)\.exe$/.test(cfg.shell),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there is also ubuntu1804.exe

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit I added these by intuition because I couldn't find a document with all the distros exectuables. I only use Debian myself. I'll add that version to the list soon.

However, as the number of distros may grow and some of them may have multiple executables (case in point), a regex is probably not the best approach. But I honestly can't think of another way to detect if Hyper is running a WSL shell.

Any other ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes actually 😄

I only know about that exe because I recently did something very similar in vscode. Check out the list I came up with https://github.com/Microsoft/vscode/blob/86adb527f12c12c1c04806cfa0cd435f98475a6f/src/vs/workbench/parts/terminal/node/windowsShellHelper.ts#L13 (which also includes powershell and cmd, everything else is wsl).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your list, I guess I have a couple of entries to fix 😄

I might follow your approach instead of regex, it will be easier to maintain in the long run.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar Changed the implementation, could you please re-review? I've also took the liberty of using path.sep instead of the hard-coded / in one other instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the code base too well so can't give a full review, the list of exes looks good though 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither, I'm just digging through code and making small changes here and there 🤣

Copy link
Contributor

@chabou chabou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would invert the way WSL is detected to be easier to maintain

'debian.exe',
'opensuse-42.exe',
'sles-12.exe'
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you transform this into something like:

const WIN_SHELLL_EXECUTABLES = [
  'cmd.exe',
  'powershell.exe'
]
function isWslShell(shell) {
  return process.platform === 'win32' && !WIN_SHELL_EXECUTABLES.indexOf(shell.split(path.sep).pop()) !== -1;
}

Be careful with empty/missing shell config.

It would be easier to maintain like @Tyriar suggested.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree, there are many other non-WSL shells: git bash, python, gchi, etc.

Copy link
Author

@rfgamaral rfgamaral May 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, either we have a full list for valid WSL shells, or non-WSL shells. Since the function is called isWslShell and the property is isWsl, I think it makes more sense to have a list of valid WSL shells. Let me know how you want me to proceed @chabou.

I'll handle the empty/missing shell configuration though :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, you're right.
But, in this case, it will take some effort to keep this list up to date.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might, it all depends on how frequent new Linux distros are going to be supported by WSL and if any of them will include different exes than the default ones. But I honestly can't think of any other way to detect if the active/running shell is WSL or not. If you have a suggestion, I'm all hears :)

@chabou
Copy link
Contributor

chabou commented May 23, 2018

Can you give some examples of plugins that need to know if current shell is running WSL or not?

@rfgamaral
Copy link
Author

@chabou Plugins like hyper-tab-icons, hypercwd and hyper-statusline will need special handling to work within WSL (they currently don't; the last 2 have open issues regarding that). There could be others out there but these are the ones I'm personally interested in using and thus fixing their WSL issues.

To give a more in-depth context, lets use hypercwd as an example.

This plugin opens new tabs with the same directory as the current tab in Hyper. It does this by reading the current tab directory with lsof on Linux and relies on the current tab directory that is printed on the Command Prompt / PowerShell on Windows (which is always the absolute path).

The Windows technique does not work for WSL because it's a Linux prompt with color codes in the mix and one could always be using a prompt configuration to show a relative path, for instance. You can't also rely on lsof because although WSL is Linux, Hyper is still running on Windows and lsof doesn't exist on Windows.

In other words, WSL needs special handling and hypercwd needs to know which method to apply to detect the absolute path for the current directory.

@rfgamaral
Copy link
Author

@chabou What do you think of the current implementation? Could we get this merged? It would help immensely to have this implemented so I could start working on fixing some plugins WSL issues.

@@ -41,7 +43,8 @@ const reducer = (state = initialState, action) => {
cols: action.cols,
rows: action.rows,
uid: action.uid,
shell: action.shell.split('/').pop(),
shell: action.shell.split(path.sep).pop(),
isWsl: action.isWsl,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you determine if this is a WSL shell here?

This is better to let plugins modify the action.shell value (in a middleware for example). In this case, this is better to call isWslShell later in the reducer than in the action creator.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be sure, are you suggesting I change this to isWsl: isWslShell(action.shell)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.
This is a derivated data and derivation should occur in the reducer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chabou There's a little problem with that I'm not sure how to handle given that I don't know the code base so well. The isWsl function was created as an app util function and this reducer is inside the lib folder. I'm not sure I can/should import stuff from app into lib (and vice-versa for that matter). How should I proceed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to move app/utils/wsl.js to lib/utils/wsl.js or move your function directly into lib/reducers/sessions.js because your function will not be used anywhere else.

@innerlee
Copy link

according to microsoft/WSL#2715 (comment), /bin/wslpath and /init are the only binaries wsl was injected into. So you can check the existence of these two files?

@rauchg rauchg added the WIP label Dec 4, 2018
@machinshin
Copy link

@rfgamaral : any updates on this thread/PR?

@Stanzilla
Copy link
Contributor

Instead of hardcoding exe names, could we do something like this instead? microsoft/terminal#1050

@Stanzilla Stanzilla closed this Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants