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

Do not replace import tokens if they are part of a snippet #5539

Merged
merged 3 commits into from
May 22, 2023

Conversation

WeidiDeng
Copy link
Member

@WeidiDeng WeidiDeng commented May 18, 2023

As discussed in slack, if an imported file also has a snippet with variadic placeholders and imports this snippet, parsing will fail saying not enough arguments.

Added tests show what the problem is.

When importing, caddy will always replace tokens placeholders. However, when importing a file that contains snippets definition, caddy will replace them. For {args[n]} type token, they will either be replaced or dropped, which is undesirable since they are part of a snippet.

Implement a naive algorithm to skip replacing tokens if they are part of a snippet. It does not check for syntax error, that will be done later when caddy finished importing all the tokens.

@WeidiDeng WeidiDeng requested a review from francislavoie May 18, 2023 06:26
@WeidiDeng
Copy link
Member Author

I cannot get rid of the warning. Don't know a good way to hide these false positives. 😞

@francislavoie
Copy link
Member

Oh, good, I like that you simplified the fix in that last commit. I thought it was weird that the tokens were passed in.

@francislavoie francislavoie added the bug 🐞 Something isn't working label May 18, 2023
@francislavoie francislavoie added this to the v2.7.0 milestone May 18, 2023
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks for the patch Weidi 😊

@WeidiDeng WeidiDeng added the deferred ⏰ We'll come back to this later label May 19, 2023
@WeidiDeng
Copy link
Member Author

@mholt This patch changes the how variadic placeholders work. Now it cannot be used outside snippets. args is an import feature, not a snippet feature. This patch fixes variadic placeholders by making them not available outside snippets.

The way to fix it requires at least 2 passes and obviously, rewriting how caddy file parsing works. What do you think of it?

@WeidiDeng WeidiDeng changed the title fix variadic placeholder in imported file which also imports Do not replace import tokens if they are part of a snippet May 21, 2023
@WeidiDeng
Copy link
Member Author

I think the problem is snippets should be exempt from the replace rule but not. Implemented a naive way to check it.

@WeidiDeng WeidiDeng removed the deferred ⏰ We'll come back to this later label May 21, 2023
@WeidiDeng WeidiDeng requested a review from mholt May 22, 2023 01:28
@mholt
Copy link
Member

mholt commented May 22, 2023

So, to be sure I'm clear on this, does this patch cause something to stop working? Based on the comments above, it sounds like using variadic placeholders, i.e. placeholders like {args[:n]} can only be used in Caddyfile snippets. Were they able to be used other places before? 🤔

@WeidiDeng
Copy link
Member Author

So, to be sure I'm clear on this, does this patch cause something to stop working? Based on the comments above, it sounds like using variadic placeholders, i.e. placeholders like {args[:n]} can only be used in Caddyfile snippets. Were they able to be used other places before? 🤔

Now they can be used in file as well, i.e. like other file imports do. There also won't be false warning about arguments index.

@mholt
Copy link
Member

mholt commented May 22, 2023

Oh, wonderful -- that sounds like a good improvement. Thank you!

@francislavoie just to double check you're good with this too? If so we can merge it 😊

Copy link
Member

@francislavoie francislavoie left a comment

Choose a reason for hiding this comment

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

LGTM!

@mholt mholt merged commit cee4441 into master May 22, 2023
@mholt mholt deleted the fix-variadic-placeholder-in-import branch May 22, 2023 21:36
@mholt
Copy link
Member

mholt commented May 22, 2023

Thanks for the contribution, @WeidiDeng !

@mholt
Copy link
Member

mholt commented Aug 3, 2023

I think this has a bug: https://caddy.community/t/problem-after-upgrading-to-2-7-2/20687/6

(matrix) {
    respond `{"m.server":"{args.0}"}`
}

localhost {
    import matrix "example.com:443"
}

With this config, the response will be example.com:443"} instead of {"m.server":"example.com:443"}.

I've observed that this only happens when the value is used in a snippet, and it does NOT happen when the JSON { is escaped (\{).

(Edit: Possibly fixed in #5685 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants