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

fix(deno): refactor to avoid prompts during module import (#2216) #2217

Merged
merged 1 commit into from Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions lib/platform-shims/deno.ts
Expand Up @@ -21,22 +21,21 @@ const REQUIRE_ERROR = 'require is not supported by ESM';
const REQUIRE_DIRECTORY_ERROR =
'loading a directory of commands is not supported yet for ESM';

const DENO_ENV_PERMITTED: boolean =
(await Deno.permissions.query({name: 'env'})).state === 'granted';
Copy link
Member

Choose a reason for hiding this comment

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

will this work even though yargs is synchronous? (have you tested?).

Copy link
Contributor Author

@rivy rivy Aug 7, 2022

Choose a reason for hiding this comment

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

I believe that you are correct in inferring that this will cause the import mainline code to execute following other purely synchronous imports. But, I believe, all imports will still execute before any module/user code. I've not seen or used any complex import recipes which would need a specific order of execution for the 'Yargs' module.

I am successfully using a fork with this patch in several user scripts, and I can post some examples. But that's just weak, "works on my machine", evidence. What would you think would help prove that this can be used safety without causing any disruptions?

relevant: Unexpected execution order (denoland/deno#14243)

const DENO_READ_CWD_PERMITTED: boolean =
(await Deno.permissions.query({name: 'read', path: '.'})).state === 'granted';

// Deno removes argv[0] and argv[1] from Deno.args:
const argv = ['deno run', ...Deno.args];
const __dirname = new URL('.', import.meta.url).pathname;

// Yargs supports environment variables with prefixes, e.g., MY_APP_FOO,
// MY_APP_BAR. Environment variables are also used to detect locale.
let cwd = '';
let env: {[key: string]: string} = {};
try {
env = Deno.env.toObject();
cwd = Deno.cwd();
} catch (err) {
if (err.name !== 'PermissionDenied') {
throw err;
}
}
const cwd = DENO_READ_CWD_PERMITTED ? Deno.cwd() : '';
const env: {[key: string]: string} = DENO_ENV_PERMITTED
? Deno.env.toObject()
: {};

const path = {
basename: basename,
Expand Down