Skip to content

Re-enable Javascript support #1347

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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bit-ignore.bazelrc

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,57 @@ Setup a simple kotlin_test.



<a id="kt_js_import"></a>

## kt_js_import

<pre>
load("@rules_kotlin//kotlin:js.bzl", "kt_js_import")

kt_js_import(<a href="#kt_js_import-name">name</a>, <a href="#kt_js_import-klibs">klibs</a>)
</pre>



**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="kt_js_import-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="kt_js_import-klibs"></a>klibs | One or more klib targets that model a kotlin/js library | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


<a id="kt_js_library"></a>

## kt_js_library

<pre>
load("@rules_kotlin//kotlin:js.bzl", "kt_js_library")

kt_js_library(<a href="#kt_js_library-name">name</a>, <a href="#kt_js_library-deps">deps</a>, <a href="#kt_js_library-srcs">srcs</a>, <a href="#kt_js_library-es_target">es_target</a>, <a href="#kt_js_library-module_kind">module_kind</a>, <a href="#kt_js_library-output_kind">output_kind</a>, <a href="#kt_js_library-sourcemap">sourcemap</a>)
</pre>



**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="kt_js_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="kt_js_library-deps"></a>deps | A list of other kt_js_library that this library depends on for compilation | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_js_library-srcs"></a>srcs | A list of source files to be transpiled to JS | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="kt_js_library-es_target"></a>es_target | - | String | optional | `"es5"` |
| <a id="kt_js_library-module_kind"></a>module_kind | - | String | optional | `"commonjs"` |
| <a id="kt_js_library-output_kind"></a>output_kind | The output to be generated with the rule, either only a klib or klib + js | String | optional | `"klib"` |
| <a id="kt_js_library-sourcemap"></a>sourcemap | Indicates whether sourcemaps (.js.map) files should be emitted if output_kind is set to js | Boolean | optional | `False` |


<!-- Generated with Stardoc: http://skydoc.bazel.build -->



<a id="ktlint_config"></a>

## ktlint_config
Expand Down
2 changes: 0 additions & 2 deletions examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ genrule(
# Exclude files in `examples`.
exclude = [
"*",
# Node is currently broken.
"node/**",
# Anvil is broken by a verison upgrade.
"anvil/**",
],
Expand Down
1 change: 1 addition & 0 deletions examples/node/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Empty file added examples/node/.bazelrc
Empty file.
1 change: 1 addition & 0 deletions examples/node/.bazelversion
2 changes: 2 additions & 0 deletions examples/node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bazel-node/
node_modules/
3 changes: 3 additions & 0 deletions examples/node/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")
26 changes: 26 additions & 0 deletions examples/node/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module(
name = "node",
)

bazel_dep(name = "protobuf", version = "29.0", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_kotlin", version = "2.1.1")
local_path_override(
module_name = "rules_kotlin",
path = "../..",
)

bazel_dep(name = "aspect_rules_js", version = "2.3.8")

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)
npm.npm_translate_lock(
name = "npm",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)
use_repo(npm, "npm")

pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")

# Allows developers to use the matching pnpm version, for example:
# bazel run -- @pnpm --dir /home/runner/work/rules_js/rules_js install
use_repo(pnpm, "pnpm")
107 changes: 107 additions & 0 deletions examples/node/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
workspace(name = "node")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "rules_kotlin",
path = "../..",
)

# required with Bazel 8/workspace
http_archive(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need rules_python?

Copy link
Author

Choose a reason for hiding this comment

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

This was needed only for this example (not the root module) with Bazel 8 workspace. It appears related to this change with Bazel 8 bazelbuild/bazel#23043. Without this, I get the error

USE_BAZEL_VERSION=8.2.1 bazel query //... --enable_workspace --noenable_bzlmod
WARNING: WORKSPACE support will be removed in Bazel 9 (late 2025), please migrate to Bzlmod, see https://bazel.build/external/migration.
ERROR: /private/var/tmp/_bazel_smocherla/bfd93a74a834f9acb8d10b4323733a2c/external/rules_python/python/py_cc_link_params_info.bzl:6:84: name 'PyCcLinkParamsProvider' is not defined
ERROR: error loading package under directory '': error loading package '': Internal error while loading Starlark builtins: Failed to autoload external symbols: compilation of module 'python/py_cc_link_params_info.bzl' failed Most likely you need to upgrade the version of rules repository in the WORKSPACE file.

It gets fixed with

USE_BAZEL_VERSION=8.2.1 bazel query //... --enable_workspace --noenable_bzlmod --incompatible_autoload_externally=-@rules_python
WARNING: WORKSPACE support will be removed in Bazel 9 (late 2025), please migrate to Bzlmod, see https://bazel.build/external/migration.
//:.aspect_rules_js/node_modules/accepts@1.3.8
//:.aspect_rules_js/node_modules/accepts@1.3.8/dir
//:.aspect_rules_js/node_modules/accepts@1.3.8/pkg
//:.aspect_rules_js/node_modules/accepts@1.3.8/ref
...

I can add that flag as well (it appears some internal bazel repos rely on rules_python) and because this is WORKSPACE, it breaks unless it's already pulled in.

Copy link
Collaborator

Choose a reason for hiding this comment

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

... python is an even bigger mess than I remembered.

name = "rules_python",
sha256 = "2ef40fdcd797e07f0b6abda446d1d84e2d9570d234fddf8fcd2aa262da852d1c",
strip_prefix = "rules_python-1.2.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/1.2.0/rules_python-1.2.0.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

# required with Bazel 8/workspace
http_archive(
name = "rules_shell",
sha256 = "bc61ef94facc78e20a645726f64756e5e285a045037c7a61f65af2941f4c25e1",
strip_prefix = "rules_shell-0.4.1",
url = "https://github.com/bazelbuild/rules_shell/releases/download/v0.4.1/rules_shell-v0.4.1.tar.gz",
)

load("@rules_shell//shell:repositories.bzl", "rules_shell_dependencies", "rules_shell_toolchains")

rules_shell_dependencies()

rules_shell_toolchains()

http_archive(
name = "rules_java",
sha256 = "6d8c6d5cd86fed031ee48424f238fa35f33abc9921fd97dd4ae1119a29fc807f",
urls = [
"https://github.com/bazelbuild/rules_java/releases/download/8.6.3/rules_java-8.6.3.tar.gz",
],
)

load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")

rules_java_dependencies()

# note that the following line is what is minimally required from protobuf for the java rules
# consider using the protobuf_deps() public API from @com_google_protobuf//:protobuf_deps.bzl
load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility

proto_bazel_features(name = "proto_bazel_features")

# register toolchains
load("@rules_java//java:repositories.bzl", "rules_java_toolchains")

rules_java_toolchains()

http_archive(
name = "rules_proto",
sha256 = "8e195dbb6a505ca4c7aafa6b7cffa47fe49a261b27a342053cfb2b973cc4aa12",
strip_prefix = "rules_proto-7.0.0",
url = "https://github.com/bazelbuild/rules_proto/releases/download/7.0.0/rules_proto-7.0.0.tar.gz",
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

rules_proto_dependencies()

load("@rules_proto//proto:setup.bzl", "rules_proto_setup")

rules_proto_setup()

http_archive(
name = "aspect_rules_js",
sha256 = "304c51726b727d53277dd28fcda1b8e43b7e46818530b8d6265e7be98d5e2b25",
strip_prefix = "rules_js-2.3.8",
url = "https://github.com/aspect-build/rules_js/releases/download/v2.3.8/rules_js-v2.3.8.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@aspect_rules_js//js:toolchains.bzl", "DEFAULT_NODE_VERSION", "rules_js_register_toolchains")

rules_js_register_toolchains(node_version = DEFAULT_NODE_VERSION)

load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

load("@rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")

kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below

load("@rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")

kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
Empty file.
15 changes: 15 additions & 0 deletions examples/node/express/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package express

@JsModule("express")
@JsNonModule
external fun express(): ExpressApp

external interface ExpressApp {
fun get(path: String, handler: (Request, Response) -> Unit)
fun listen(port: Int, callback: () -> Unit)
}

external interface Request
external interface Response {
fun send(body: String)
}
23 changes: 23 additions & 0 deletions examples/node/express/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary")
load("@rules_kotlin//kotlin:js.bzl", "kt_js_library")

kt_js_library(
name = "app",
srcs = [
"App.kt",
"Main.kt",
],
output_kind = "js",
)

filegroup(
name = "js-file",
srcs = [":app"],
output_group = "js",
)

js_binary(
name = "express",
data = ["//:node_modules/express"],
entry_point = ":js-file",
)
13 changes: 13 additions & 0 deletions examples/node/express/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package express

fun main() {
val app = express()

app.get("/") { _, res ->
res.send("Hello from Kotlin/JS Node server!")
}

app.listen(3000) {
println("Server started on http://localhost:3000")
}
}
10 changes: 10 additions & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "node",
"version": "1.0.0",
"dependencies": {
"express": "^4.19.2"
},
"devDependencies": {
"source-map-support": "^0.5.6"
}
}
Loading