Skip to content

Commit

Permalink
fix!: Don't cd into build.location. Require tree-sitter CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 20, 2024
1 parent a457651 commit 561eecf
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 72 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ build = {
---@type string (required) Name of the language, e.g. "haskell".
lang = "LANG",

--- Will use `tree-sitter build` if these are unset or empty.
---@type string[]? (optional) parser source files.
sources = { "src/parser.c", "src/scanner.c" },

---@type boolean? (optional) Won't build the parser if `false`.
parser = true,

Expand Down
1 change: 0 additions & 1 deletion fixtures/tree-sitter-haskell-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ source = {
build = {
type = "treesitter-parser",
lang = "haskell",
sources = { "src/parser.c", "src/scanner.c" },
copy_directories = { "queries" },
queries = {
["folds.scm"] = [==[
Expand Down
1 change: 0 additions & 1 deletion fixtures/tree-sitter-latex-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ dependencies = {
build = {
type = "treesitter-parser",
lang = "latex",
sources = { "src/parser.c", "src/scanner.c" },
generate = true,
}
1 change: 0 additions & 1 deletion fixtures/tree-sitter-ocamllex-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ dependencies = {
build = {
type = "treesitter-parser",
lang = "ocamllex",
sources = { "src/parser.c", "src/scanner.c" },
generate = true,
}
1 change: 0 additions & 1 deletion fixtures/tree-sitter-rust-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies = {
build = {
type = "treesitter-parser",
lang = "rust",
sources = { "src/parser.c", "src/scanner.c" },
queries = {
["highlights.scm"] = [[
;;
Expand Down
1 change: 0 additions & 1 deletion fixtures/tree-sitter-toml-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies = {
build = {
type = "treesitter-parser",
lang = "toml",
sources = { "src/parser.c", "src/scanner.c" },
generate = true,
generate_from_json = true,
}
1 change: 0 additions & 1 deletion fixtures/tree-sitter-xml-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ dependencies = {
build = {
type = "treesitter-parser",
lang = "xml",
sources = { "src/parser.c", "src/scanner.c" },
location = "xml",
}
52 changes: 34 additions & 18 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
knownRockspec = "${self}/luarocks-build-treesitter-parser-scm-1.rockspec";
src = self;
disabled = luaOlder "5.1";
propagatedBuildInputs = [
luafilesystem
];
propagatedBuildInputs =
[
luafilesystem
]
++ (with final; [
tree-sitter
gcc
]);
}) {};

tree-sitter-haskell =
Expand All @@ -35,12 +40,8 @@
};
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(realpath .)
export HOME=$(mktemp -d)
'';
buildInputs = with final; [
gcc
tree-sitter
];
propagatedBuildInputs = [
luarocks-build-treesitter-parser
];
Expand Down Expand Up @@ -81,6 +82,10 @@
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(mktemp -d)
'';
}) {})
.overrideAttrs (oa: {
fixupPhase = ''
Expand Down Expand Up @@ -111,13 +116,16 @@
hash = "sha256-YhmEE7I7UF83qMuldHqc/fD/no/7YuZd6CaAIaZ1now=";
};
buildInputs = [
tree-sitter
nodejs_22
];
propagatedBuildInputs = [
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(mktemp -d)
'';
}) {};

tree-sitter-toml = luaself.callPackage ({
Expand All @@ -139,13 +147,16 @@
hash = "sha256-z9MWNOBxLHBd/pVs5/QiSSGtaW+DUd7y3wZXcl3hWnk=";
};
buildInputs = [
tree-sitter
nodejs_22
];
propagatedBuildInputs = [
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(mktemp -d)
'';
}) {};

tree-sitter-latex = luaself.callPackage ({
Expand All @@ -167,13 +178,16 @@
hash = "sha256-ptUIi8cMQ4CrnqZgnzJ0rnByd78y8l607+CSPKNrLEk=";
};
buildInputs = [
tree-sitter
nodejs_22
];
propagatedBuildInputs = [
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(mktemp -d)
'';
}) {};

tree-sitter-xml = luaself.callPackage ({
Expand All @@ -196,6 +210,10 @@
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(mktemp -d)
'';
}) {};

tree-sitter-norg =
Expand All @@ -217,15 +235,12 @@
};
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(realpath .)
export HOME=$(mktemp -d)
'';
propagatedBuildInputs = [
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
buildInputs = [
final.tree-sitter
];
fixupPhase = ''
if [ ! -f $out/lib/lua/5.1/parser/norg.so ]; then
echo "Build did not create parser/norg.so in the expected location"
Expand Down Expand Up @@ -272,9 +287,10 @@
luarocks-build-treesitter-parser
];
disabled = luaOlder "5.1";
buildInputs = [
final.tree-sitter
];
preBuild = ''
# tree-sitter CLI expects to be able to create log files, etc.
export HOME=$(realpath .)
'';
}) {};
};
lua5_1_base =
Expand Down
59 changes: 15 additions & 44 deletions src/luarocks/build/treesitter-parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local fs = require("luarocks.fs")
local dir = require("luarocks.dir")
local path = require("luarocks.path")
local builtin = require("luarocks.build.builtin")
local util = require("luarocks.util")
local cfg = require("luarocks.core.cfg")

Expand All @@ -23,7 +22,6 @@ local treesitter_parser = {}

---@class TreeSitterBuildSpec: BuildSpec
---@field lang string
---@field sources? string[]
---@field parser? boolean
---@field libflags? string[]
---@field generate? boolean
Expand All @@ -40,7 +38,6 @@ local function execute(...)
end

---@param rockspec table
---@param no_install boolean
function treesitter_parser.run(rockspec, no_install)
assert(rockspec:type() == "rockspec")
---@cast rockspec RockSpec
Expand All @@ -50,9 +47,9 @@ function treesitter_parser.run(rockspec, no_install)
local build = rockspec.build

local build_parser = build.parser == nil or build.parser
local has_sources = type(build.sources) == "table" and #build.sources > 0

if (build.generate or not has_sources) and not fs.is_tool_available("tree-sitter", "tree-sitter CLI") then
-- TODO Make sure tree-sitter CLI version >= 0.22.2
if not fs.is_tool_available("tree-sitter", "tree-sitter CLI") then
return nil,
"'tree-sitter CLI' is not installed.\n" .. rockspec.name .. " requires the tree-sitter CLI to build.\n"
end
Expand All @@ -64,21 +61,19 @@ function treesitter_parser.run(rockspec, no_install)
("'%s' is not installed.\n%s requires %s to build."):format(js_runtime, rockspec.name, js_runtime_name)
end
end
if build.location then
util.printout("Changing to directory: " .. build.location)
fs.change_dir(build.location)
end
if build.generate then
local cmd
cmd = { "tree-sitter", "generate", "--no-bindings" }
local abi = os.getenv("TREE_SITTER_LANGUAGE_VERSION")
-- TODO: Check for tree-sitter CLI version
if abi then
table.insert(cmd, "--abi")
table.insert(cmd, abi)
end
if build.generate_from_json then
table.insert(cmd, "src/grammar.json")
local src_dir = build.location and dir.path(build.location, "src") or "src"
table.insert(cmd, dir.path(src_dir, "grammar.json"))
elseif build.location then
table.insert(cmd, dir.path(build.location, "grammar.js"))
end
util.printout("Generating tree-sitter sources...")
local cmd_str = table.concat(cmd, " ")
Expand All @@ -88,24 +83,6 @@ function treesitter_parser.run(rockspec, no_install)
end
util.printout("Done.")
end
local incdirs, is_cpp = {}, false
for _, source in ipairs(build.sources or {}) do
local source_dir = source:match("(.-)%/")
is_cpp = is_cpp
or source:match("%.cc$") ~= nil
or source:match("%.cpp$") ~= nil
or source:match("%.cxx$") ~= nil
if dir then
table.insert(incdirs, source_dir)
end
end
if is_cpp and not rockspec.build.libflags then
local prev = rockspec.variables.LIBFLAG
rockspec.variables.LIBFLAG = prev .. (prev and #prev > 1 and " " or "") .. "-lstdc++"
end
if rockspec.build.libflags then
rockspec.variables.LIBFLAG = table.concat(rockspec.build.libflags, " ")
end
if build.queries then
if fs.is_dir("queries") then
pcall(fs.delete, "queries")
Expand All @@ -131,30 +108,24 @@ function treesitter_parser.run(rockspec, no_install)
rockspec.build.copy_directories = rockspec.build.copy_directories or {}
table.insert(rockspec.build.copy_directories, "queries")
end
if has_sources then
rockspec.build.modules = {
["parser." .. build.lang] = {
sources = build.sources,
incdirs = incdirs,
},
}
end
local lib_dir = path.lib_dir(rockspec.name, rockspec.version)
local parser_dir = dir.path(lib_dir, "parser")
local parser_dir = no_install and "luarocks_build" or dir.path(lib_dir, "parser")
local ok, err
if build_parser then
local parser_lib = rockspec.build.lang .. "." .. cfg.lib_extension
if fs.is_tool_available("tree-sitter", "tree-sitter CLI") then
-- Try tree-sitter build first
fs.make_dir(parser_dir)
local parser_lib_path = dir.path(parser_dir, parser_lib)
ok = execute("tree-sitter", "build", "-o", parser_lib_path, ".") and fs.exists(parser_lib_path)
local src_dir = build.location and build.location or fs.current_dir()
ok = execute("tree-sitter", "build", "-o", parser_lib_path, src_dir) and fs.exists(parser_lib_path)
end
if not ok and has_sources then
-- Fall back to builtin build
ok, err = builtin.run(rockspec, no_install)
elseif not ok then
err = "'tree-sitter build' failed. Note: tree-sitter 0.22.2 or later is required to build this parser."
if not ok then
err = [[
'tree-sitter build' failed.
See the build output for details.
Note: tree-sitter 0.22.2 or later is required to build this parser.
]]
end
pcall(function()
local dsym_file = dir.absolute_name(dir.path(parser_dir, parser_lib .. ".dSYM"))
Expand Down

0 comments on commit 561eecf

Please sign in to comment.