bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
14.81k stars 1.24k forks source link

component-macro: normalize the path parameter in component bindgen #8871

Closed iawia002 closed 6 days ago

iawia002 commented 1 week ago

I encountered a file path issue while using the wasmtime::component::bindgen! macro, I used a relative file path (e.g., ../wit) in the path parameter, and then I received an error message:

failed to read path for WIT [/Users/code/test-component/examples/../wit]

The file path in the error message was quite confusing, in reality, the path that was not found should be /Users/code/test-component/wit. The relative file path in the error message made it hard for me to immediately understand which directory wasmtime::component::bindgen! was using to look for WIT files. Therefore, this patch normalizes the path before resolving WIT files.

Error message before:

failed to read path for WIT [/Users/code/test-component/examples/../wit]

after:

failed to read path for WIT [/Users/code/test-component/wit]

The wit_bindgen::generate! macro has the same issue. If this patch is reasonable, I will make the same changes to wit_bindgen::generate! afterward.

alexcrichton commented 1 week ago

Thanks for the PR! Personally though I find path manipulation very tricky/subtle. For example in the presence of symlinks I'm not sure that this implementation is correct. In lieu of that perhaps std::fs::canonicalize could be used instead? If that returns Ok then the resulting path can be used and if it returns Err then the original path can be used as-is to probably return an error somewhere else.

iawia002 commented 1 week ago

Thanks for the review, I agree that path manipulation is tricky, std::fs::canonicalize is more of a compromise solution. It can handle some scenarios (such as when the path itself exists but the wit file does not). However, if the path itself does not exist, the error message will still be the same as it is now. But I believe this is acceptable because we would rather not incorrectly normalize the path in some corner cases.