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: Don't replace malformed appmap.yml #125

Merged
merged 1 commit into from
Apr 3, 2024

Conversation

zermelo-wisen
Copy link
Collaborator

Fixes #80.

src/config.ts Outdated
const config = tryOr(() => YAML.parse(readFileSync(path, "utf8")) as unknown);
const fileContent = tryOr(() => readFileSync(path, "utf8"));
if (!fileContent) return;
const config = tryOr(() => YAML.parse(fileContent) as unknown, { malformed: true });
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a weird approach. Can you try {...} catch { return "malformed" } instead? readConfigFile would return ConfigFile | "malformed" | undefined and you could check for that instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, at first, I thought about making it so, but later, I thought it leads to needless changes in surrounding parts and switched to this one. Now I changed it to ConfigFile | "malformed" | undefined return.

Copy link
Collaborator

@dividedmind dividedmind left a comment

Choose a reason for hiding this comment

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

I gave this some more thought, and I think it would be good to expand the scope a bit; in particular, this will still lead to replacing the config file on eg. EACCES error, which can be a problem.

Perhaps readConfigFile should just rethrow any exceptions except ENOENT (which would return undefined). The user can then be warned with the specific error message to make it clearer what's going on.

src/config.ts Outdated
@@ -94,12 +97,16 @@ interface ConfigFile {
name?: string;
packages?: Package[];
response_body_max_length?: number;
malformed?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you forgot to remove this.

@zermelo-wisen
Copy link
Collaborator Author

zermelo-wisen commented Mar 19, 2024

I gave this some more thought, and I think it would be good to expand the scope a bit; in particular, this will still lead to replacing the config file on eg. EACCES error, which can be a problem.

Perhaps readConfigFile should just rethrow any exceptions except ENOENT (which would return undefined). The user can then be warned with the specific error message to make it clearer what's going on.

Ok, If they have a config file besides that they don't want it to be replaced, they don't want to use default parameters, too. So we don't go on with the default config if it throws any exception except ENOENT. User will need to resolve the specific error with their config file in order to proceed.

What about exiting when it's malformed too?

@zermelo-wisen zermelo-wisen force-pushed the fix/malformed-appmap-yml-replacement branch from 62d0093 to d96cf47 Compare March 19, 2024 13:31
@dividedmind
Copy link
Collaborator

I gave this some more thought, and I think it would be good to expand the scope a bit; in particular, this will still lead to replacing the config file on eg. EACCES error, which can be a problem.
Perhaps readConfigFile should just rethrow any exceptions except ENOENT (which would return undefined). The user can then be warned with the specific error message to make it clearer what's going on.

Ok, If they have a config file besides that they don't want it to be replaced, they don't want to use default parameters, too. So we don't go on with the default config if it throws any exception except ENOENT. User will need to resolve the specific error with their config file in order to proceed.

What about exiting when it's malformed too?

Yeah, perhaps you're right. Maybe we should just let the exceptions propagate and catch ENOENT only in Config constructor. I'm starting to think that will be the least surprising. What do you think?

@zermelo-wisen
Copy link
Collaborator Author

I gave this some more thought, and I think it would be good to expand the scope a bit; in particular, this will still lead to replacing the config file on eg. EACCES error, which can be a problem.
Perhaps readConfigFile should just rethrow any exceptions except ENOENT (which would return undefined). The user can then be warned with the specific error message to make it clearer what's going on.

Ok, If they have a config file besides that they don't want it to be replaced, they don't want to use default parameters, too. So we don't go on with the default config if it throws any exception except ENOENT. User will need to resolve the specific error with their config file in order to proceed.
What about exiting when it's malformed too?

Yeah, perhaps you're right. Maybe we should just let the exceptions propagate and catch ENOENT only in Config constructor. I'm starting to think that will be the least surprising. What do you think?

I agree. One point that makes me think is that the user should be given the opportunity to run with defaults if they struggle to fix their config file. For this, maybe we should inform them to remove (backup) or rename their config file to run with defaults. Or we can introduce a flag like --ignore-config-file or --use-defaults, and let the user know they can use these.

@dividedmind
Copy link
Collaborator

dividedmind commented Mar 26, 2024

I gave this some more thought, and I think it would be good to expand the scope a bit; in particular, this will still lead to replacing the config file on eg. EACCES error, which can be a problem.
Perhaps readConfigFile should just rethrow any exceptions except ENOENT (which would return undefined). The user can then be warned with the specific error message to make it clearer what's going on.

Ok, If they have a config file besides that they don't want it to be replaced, they don't want to use default parameters, too. So we don't go on with the default config if it throws any exception except ENOENT. User will need to resolve the specific error with their config file in order to proceed.
What about exiting when it's malformed too?

Yeah, perhaps you're right. Maybe we should just let the exceptions propagate and catch ENOENT only in Config constructor. I'm starting to think that will be the least surprising. What do you think?

I agree. One point that makes me think is that the user should be given the opportunity to run with defaults if they struggle to fix their config file. For this, maybe we should inform them to remove (backup) or rename their config file to run with defaults. Or we can introduce a flag like --ignore-config-file or --use-defaults, and let the user know they can use these.

Nah, I don't think the flag is necessary. But I like the idea of explaining the problem and indicating the possible solution, eg. with a message like Error parsing config file at ${path}: ${error.message}.\nYou can remove the file to use the default configuration..

Now I changed it to re-throw when malformed too, with the message above.

@zermelo-wisen zermelo-wisen force-pushed the fix/malformed-appmap-yml-replacement branch from d96cf47 to d244d0c Compare March 26, 2024 09:51
@zermelo-wisen zermelo-wisen changed the title fix: Don't replace malformed appmap.yml, warn and use defaults fix: Don't replace malformed appmap.yml Mar 26, 2024
src/config.ts Outdated
config = YAML.parse(fileContent) as unknown;
} catch (exn) {
const exnMessage =
exn && typeof exn == "object" && "message" in exn && typeof exn.message == "string"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can just assert(isNativeError(exn)) for simplicity :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@@ -1,7 +1,7 @@
export default function tryOr<T>(fn: () => T): T | undefined {
export default function tryOr<T, E = undefined>(fn: () => T, elseReturn?: E): T | E | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this change is used anymore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted

@zermelo-wisen zermelo-wisen force-pushed the fix/malformed-appmap-yml-replacement branch from d244d0c to c8a2caf Compare March 29, 2024 07:26
- Use defaults only when there is no config file (ENOENT).
- Rethrow every other error thrown from readFileSync (ex: EACCESS).
- Rethrow YAML parsing error when appmap.yml is malformed, appending
  an informative message.
@zermelo-wisen zermelo-wisen force-pushed the fix/malformed-appmap-yml-replacement branch from c8a2caf to 27d9b44 Compare March 29, 2024 07:28
@dividedmind dividedmind merged commit 48df24c into main Apr 3, 2024
6 checks passed
@dividedmind dividedmind deleted the fix/malformed-appmap-yml-replacement branch April 3, 2024 13:02
@appland-release
Copy link

🎉 This PR is included in version 2.19.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

Malformed appmap.yml is replaced on start
3 participants