Skip to content

Commit

Permalink
fix(*) improve Lua paths for dev environments (Kong#2845)
Browse files Browse the repository at this point in the history
Historically, and so far, with success, the Lua path was always setup so
that when in the `kong/` repository, the local files take precedence over
any system-wide installation of code. This includes all Lua files in
this repository, in the following environments:

- The Kong CLI
- Running Kong instances
- Running tests suites

Prior to this, we were having several issues with some disparities in
the Lua path setups. Disparities that became more apparent after some
recent changes, possibly related to the Homebrew Formula updates made
with the prefix and the LuaRocks system tree.

We here attempt to unify the various places in which we specify the Lua
path values for each of the aforementioned environments.

Following this change, our Lua path should look like so in all
environments:

    # inserted by bin/kong in CLI and lua_package_path in configuration
    # used for development
    ./?.lua;
    ./?/init.lua;
    # inserted by OpenResty (lua-resty-core in lualib + unused site)
    /usr/local/Cellar/openresty/1.11.2.4/site/lualib/?.lua;
    /usr/local/Cellar/openresty/1.11.2.4/site/lualib/?/init.lua;
    /usr/local/Cellar/openresty/1.11.2.4/lualib/?.lua;
    /usr/local/Cellar/openresty/1.11.2.4/lualib/?/init.lua;
    # inserted by LuaRocks (user tree)
    /Users/chasum/.luarocks/share/lua/5.1/?.lua;
    /Users/chasum/.luarocks/share/lua/5.1/?/init.lua;
    # inserted by LuaRocks (system tree used by Homebrew)
    /usr/local/opt/kong/share/lua/5.1/?.lua;
    /usr/local/opt/kong/share/lua/5.1/?/init.lua;
    # Inserted by LuaRocks (luarocks rocks is installed there)
    /usr/local/Cellar/luarocks/2.4.2/share/lua/5.1/?.lua;
    # A default path from the VM
    ./?.lua;
    # LuaJIT introspection lib
    /usr/local/Cellar/openresty/1.11.2.4/luajit/share/luajit-2.1.0-beta2/?.lua;
    # Another default path from the VM I think
    /usr/local/share/lua/5.1/?.lua;
    /usr/local/share/lua/5.1/?/init.lua;
    # Yet another default path from the VM (I think)
    # <INSTALL_PREFIX>/share/lua/<VERSION>
    /usr/local/Cellar/openresty/1.11.2.4/luajit/share/lua/5.1/?.lua;
    /usr/local/Cellar/openresty/1.11.2.4/luajit/share/lua/5.1/?/init.lua;

Note: to avoid having to keep using the `kong/?.lua` value which was
only being used to load the main `kong.lua` module, we had two possible
approaches: either change all of the `require "kong"` statements, which
would be breaking because have to update the Nginx template, either
finally make the main module adhere to our `init.lua` convention,
already followed across the codebase.
  • Loading branch information
thibaultcha authored and kikito committed Aug 24, 2017
1 parent 5d58a43 commit cce9b97
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .busted
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
return {
default = {
lpath = "./?.lua;"
lpath = "./?.lua;./?/init.lua;"
}
}
2 changes: 0 additions & 2 deletions bin/busted
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ require("kong.core.globalpatches")({
rbusted = true
})

package.path = "?/init.lua;"..package.path

-- Busted command-line runner
require 'busted.runner'({ standalone = false })
2 changes: 1 addition & 1 deletion bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require "luarocks.loader"

package.path = "?/init.lua;"..package.path
package.path = "./?.lua;./?/init.lua;" .. package.path

require("kong.cmd.init")(arg)
2 changes: 1 addition & 1 deletion kong-0.11.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = {
build = {
type = "builtin",
modules = {
["kong"] = "kong/kong.lua",
["kong"] = "kong/init.lua",
["kong.meta"] = "kong/meta.lua",
["kong.constants"] = "kong/constants.lua",
["kong.singletons"] = "kong/singletons.lua",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ lua_code_cache = on
lua_socket_pool_size = 30
lua_ssl_trusted_certificate = NONE
lua_ssl_verify_depth = 1
lua_package_path = ?/init.lua;./kong/?.lua
lua_package_path = ./?.lua;./?/init.lua;
lua_package_cpath = NONE
]]
2 changes: 1 addition & 1 deletion spec/01-unit/003-prefix_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("NGINX conf compiler", function()
describe("compile_kong_conf()", function()
it("compiles the Kong NGINX conf chunk", function()
local kong_nginx_conf = prefix_handler.compile_kong_conf(helpers.test_conf)
assert.matches("lua_package_path '?/init.lua;./kong/?.lua;;';", kong_nginx_conf, nil, true)
assert.matches("lua_package_path './?.lua;./?/init.lua;;;'", kong_nginx_conf, nil, true)
assert.matches("lua_code_cache on;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9000;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9001;", kong_nginx_conf, nil, true)
Expand Down
2 changes: 0 additions & 2 deletions spec/kong_tests.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ anonymous_reports = off

dns_hostsfile = spec/fixtures/hosts

lua_package_path = ?/init.lua;./kong/?.lua

nginx_worker_processes = 1
nginx_optimizations = off

Expand Down

0 comments on commit cce9b97

Please sign in to comment.