-
Notifications
You must be signed in to change notification settings - Fork 45
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
internal: KCL modules, part 1 #4149
Conversation
QA Wolf here! As you write new code it's important that your test coverage is keeping up. |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4149 +/- ##
==========================================
- Coverage 85.99% 85.83% -0.16%
==========================================
Files 76 76
Lines 26700 27155 +455
==========================================
+ Hits 22960 23309 +349
- Misses 3740 3846 +106
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I'm pretty unfamiliar with the engine and program memory code, so I'd want to get Adam to take a look too.
src/wasm-lib/kcl/src/executor.rs
Outdated
/// this is empty, we're not currently executing a use statement. | ||
pub import_stack: Vec<std::path::PathBuf>, | ||
/// The directory of the current project. This is used for resolving import | ||
/// paths. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you document when it can be None and what that means? Depending on the answer, it might be better to use a custom enum rather than Option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume 'none' means current working directory but yeah should be documented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an enum, so hopefully it's clearer. 282225c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, the ProjectDirectory
enum that I made is giving me trouble with wasm. I get the error "the trait FromWasmAbi
is not implemented for ProjectDirectory". wasm_bindgen
doesn't support them rustwasm/wasm-bindgen#2407.
How are we able to use enums with values elsewhere, like here?
modeling-app/src/wasm-lib/kcl/src/ast/types.rs
Lines 453 to 461 in fbac993
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)] | |
#[databake(path = kcl_lib::ast::types)] | |
#[ts(export)] | |
#[serde(tag = "type")] | |
pub enum BodyItem { | |
ExpressionStatement(ExpressionStatement), | |
VariableDeclaration(VariableDeclaration), | |
ReturnStatement(ReturnStatement), | |
} |
I reverted back to the Option<String>
and added a comment explaining None.
@@ -178,7 +178,7 @@ async fn snapshot_endpoint(body: Bytes, state: ExecutorContext) -> Response<Body | |||
// Let users know if the test is taking a long time. | |||
let (done_tx, done_rx) = oneshot::channel::<()>(); | |||
let timer = time_until(done_rx); | |||
let snapshot = match state.execute_and_prepare_snapshot(&program, id_generator).await { | |||
let snapshot = match state.execute_and_prepare_snapshot(&program, id_generator, None).await { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you avoid modifying the execute_and_prepare_snapshot
method, this PR diff will be much smaller (just make a new method that lets you take the optional param instead, and make the existing one set it to None)
src/wasm-lib/kcl/src/executor.rs
Outdated
/// this is empty, we're not currently executing a use statement. | ||
pub import_stack: Vec<std::path::PathBuf>, | ||
/// The directory of the current project. This is used for resolving import | ||
/// paths. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume 'none' means current working directory but yeah should be documented
source_ranges: vec![import_stmt.into()], | ||
})); | ||
} | ||
let source = self.fs.read_to_string(&resolved_path, source_range).await?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't have to be in this PR, but eventually, we'll want to cache the "read, parse, evaluate file" logic. If multiple files all import a shared library like helpers.kcl
or polygon.kcl
then we'll want to avoid re-parsing them.
if !module_exports.contains(&import_item.name.name) { | ||
return Err(KclError::Semantic(KclErrorDetails { | ||
message: format!( | ||
"Cannot import \"{}\" from module because it is not exported. Add \"export\" before the definition to export it.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually (doesn't have to be in this PR) it'd be good to check for export-violation before we evaluate it -- just to return errors quicker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love if we had a simple way to extract all the exported identifiers (and their type?) without evaluating anything. But yeah, I don't think we're there yet.
Addresses #4080. (Not ready to close it yet.)
Important
Requires a fix for #4147 before it can work in ZMA.
Overview
This also implements multiple imports with optional renaming.
Note: Imported files must be in the same directory.
Things for a follow-up PR:
import
anywhere besides the top level. Needs parser restructuring to track the context of a "function body".export
anywhere besides the top level. It has no effect, but we should tell people it's not valid instead of silently ignoring it.