Skip to content
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

console: autorequire modules suitable for administration and debugging #9986

Closed
Totktonada opened this issue Apr 27, 2024 · 4 comments · Fixed by #10014
Closed

console: autorequire modules suitable for administration and debugging #9986

Totktonada opened this issue Apr 27, 2024 · 4 comments · Fixed by #10014
Labels
3.2 Target is 3.2 and all newer release/master branches console feature A new functionality

Comments

@Totktonada
Copy link
Member

Totktonada commented Apr 27, 2024

It is convenient to have some built-in modules accessible right after connection to the admin console (using tt connect) or after starting a debugging session (using tarantool -i).

These are ones from where an admin wants to obtain state information: compat, fiber.info(), config:info(), config:get() are such examples. fun is often used to filter/transform such information.

There are modules that implement a data type and they're used to write a value of the given type to the database: uuid, datetime, decimal, varbinary.

Also, some modules are often used in interactive experimenting: clock.bench(), ffi.cast() and serializers such as json, yaml, msgpack.

There are function that are useful for debugging: fio.open(<...>):read(), log.info(), popen.shell().

The proposed list of modules:

  • clock
  • compat
  • config
  • datetime
  • decimal
  • ffi
  • fiber
  • fio
  • fun
  • json
  • log
  • msgpack
  • popen
  • uuid
  • varbinary
  • yaml

I propose to don't add these names to globals, but make them accessible only in the interactive console: a code in the Lua files still need to require the modules. It is easy to do after implementing console session scoped variables, see #9985.

I propose to autorequire the modules in the interactive console session under the same console_session_scope_vars compat option.

@Totktonada Totktonada added feature A new functionality console labels Apr 27, 2024
@sergos
Copy link
Contributor

sergos commented May 3, 2024

I feel uneasy because of inconsistency between the console mode and the lua script mode. Namely, things one tried in console doesn't work in a script passed as an argument to the Tarantool.

@Totktonada
Copy link
Member Author

@sergos I agree with your point in context of some other differences. For example, #1133 is the great addition that makes the interactive mode quite similar to a Lua file code. As another example, I would like to save locals within a console session to ease copy-paste from Lua files to console. (A usual function in a Lua file has local.)

My proposal adds the difference deliberately. Moreover, the goal is to have these shortcuts for the interactive work and have them only in the interactive mode.

Let's look on your doubt more precisely: consider a bad scenario. An application developer wrote some code in a console, debugged it, then copy-pasted it to a Lua file (BTW, I often write code this way).

Let's assume that some of the used modules are not require'd.

The copy-pasted code has the following properties:

  • luacheck warns about a read of an undeclared global.
  • The strict mode raises an error on attempt to access an undefined global.
  • If the strict ode is disabled, an attempt to use the module (call a function from it) leads to the 'attempt to index nil value' error.

The 'forgot to add require' is a typical mistake and it occurs disregarding whether the interactive console allows to omit some of them.

I don't think that we add any UX problem here.

And I'm sure that it makes the interactive debugging/administration more convenient.

@Totktonada
Copy link
Member Author

Feedback from @Mons: it would be good to allow to configure the list of default console upvalues.

I propose the following API.

local console = require('console')

-- A Lua table, filled by the modules as describe above.
console.default_upvalues

-- Refuse the autorequire for a particular module.
console.default_upvalues.fio = nil

-- Add a module to the autorequire list.
console.default_upvalues.mymodule = require('mymodule')

The console.default_upvalues table is to be copyed into a console session.

@Mons
Copy link
Contributor

Mons commented May 4, 2024

I feel uneasy because of inconsistency between the console mode and the lua script mode. Namely, things one tried in console doesn't work in a script passed as an argument to the Tarantool.

There are a lot of runtimes, where imports are omitted and implied for easiness of writing code by hand. And barely someone is annoyed when in the leetcode console they have everything imported, and while copied to the script they require additional imports.

Totktonada added a commit to Totktonada/tarantool that referenced this issue May 15, 2024
Fixes tarantool#9986

@TarantoolBot document
Title: Interactive console now autorequires a couple of built-in modules

There are built-in modules that are frequently used for administration
or debugging purposes. It is convenient to have them accessible in the
interactive console without extra actions.

They're accessible now without a manual `require` call if the
`console_session_scope_vars` compat option is set to `new` (see also
tarantool/doc#4191).

The list of the autorequired modules is below.

* clock
* compat
* config
* datetime
* decimal
* fiber
* fio
* fun
* json
* log
* msgpack
* popen
* uuid
* varbinary
* yaml

See tarantool#9986 for motivation behind this feature.

This list forms so called initial environment for an interactive console
session. The default initial environment may be adjusted by an
application, for example, to include application specific administrator
functions.

Two public functions are added for this purpose: `console.initial_env()`
and `console.set_initial_env(env)`.

Example 1 (keep autorequired modules, but add one more variable):

```lua
local console = require('console')

-- Add myapp_info function.
local initial_env = console.initial_env()
initial_env.myapp_info = function()
    <...>
end
```

Example 2 (replace the whole initial environment):

```lua
local console = require('console')

-- Add myapp_info function, discard the autorequired modules.
console.set_initial_env({
    myapp_info = function()
        <...>
    end,
})
```

The `console.set_initial_env()` call without an argument or with a `nil`
argument drops the initial environment to its default.

A modification of the initial environment doesn't affect existing
console sessions. It affects console sessions that are created
after the modification.

Please, adjust the `console_session_scope_vars` compat option
description and extend the built-in `console` module reference with the
new functions.
Totktonada added a commit to Totktonada/tarantool that referenced this issue May 16, 2024
Fixes tarantool#9986

@TarantoolBot document
Title: Interactive console now autorequires a couple of built-in modules

There are built-in modules that are frequently used for administration
or debugging purposes. It is convenient to have them accessible in the
interactive console without extra actions.

They're accessible now without a manual `require` call if the
`console_session_scope_vars` compat option is set to `new` (see also
tarantool/doc#4191).

The list of the autorequired modules is below.

* clock
* compat
* config
* datetime
* decimal
* ffi
* fiber
* fio
* fun
* json
* log
* msgpack
* popen
* uuid
* varbinary
* yaml

See tarantool#9986 for motivation behind this feature.

This list forms so called initial environment for an interactive console
session. The default initial environment may be adjusted by an
application, for example, to include application specific administrator
functions.

Two public functions are added for this purpose: `console.initial_env()`
and `console.set_initial_env(env)`.

Example 1 (keep autorequired modules, but add one more variable):

```lua
local console = require('console')

-- Add myapp_info function.
local initial_env = console.initial_env()
initial_env.myapp_info = function()
    <...>
end
```

Example 2 (replace the whole initial environment):

```lua
local console = require('console')

-- Add myapp_info function, discard the autorequired modules.
console.set_initial_env({
    myapp_info = function()
        <...>
    end,
})
```

The `console.set_initial_env()` call without an argument or with a `nil`
argument drops the initial environment to its default.

A modification of the initial environment doesn't affect existing
console sessions. It affects console sessions that are created
after the modification.

Please, adjust the `console_session_scope_vars` compat option
description and extend the built-in `console` module reference with the
new functions.
Totktonada added a commit to Totktonada/tarantool that referenced this issue May 17, 2024
Fixes tarantool#9986

@TarantoolBot document
Title: Interactive console now autorequires a couple of built-in modules

There are built-in modules that are frequently used for administration
or debugging purposes. It is convenient to have them accessible in the
interactive console without extra actions.

They're accessible now without a manual `require` call if the
`console_session_scope_vars` compat option is set to `new` (see also
tarantool/doc#4191).

The list of the autorequired modules is below.

* clock
* compat
* config
* datetime
* decimal
* ffi
* fiber
* fio
* fun
* json
* log
* msgpack
* popen
* uuid
* varbinary
* yaml

See tarantool#9986 for motivation behind this feature.

This list forms so called initial environment for an interactive console
session. The default initial environment may be adjusted by an
application, for example, to include application specific administrator
functions.

Two public functions are added for this purpose: `console.initial_env()`
and `console.set_initial_env(env)`.

Example 1 (keep autorequired modules, but add one more variable):

```lua
local console = require('console')

-- Add myapp_info function.
local initial_env = console.initial_env()
initial_env.myapp_info = function()
    <...>
end
```

Example 2 (replace the whole initial environment):

```lua
local console = require('console')

-- Add myapp_info function, discard the autorequired modules.
console.set_initial_env({
    myapp_info = function()
        <...>
    end,
})
```

The `console.set_initial_env()` call without an argument or with a `nil`
argument drops the initial environment to its default.

A modification of the initial environment doesn't affect existing
console sessions. It affects console sessions that are created
after the modification.

Please, adjust the `console_session_scope_vars` compat option
description and extend the built-in `console` module reference with the
new functions.
Totktonada added a commit that referenced this issue May 17, 2024
Fixes #9986

@TarantoolBot document
Title: Interactive console now autorequires a couple of built-in modules

There are built-in modules that are frequently used for administration
or debugging purposes. It is convenient to have them accessible in the
interactive console without extra actions.

They're accessible now without a manual `require` call if the
`console_session_scope_vars` compat option is set to `new` (see also
tarantool/doc#4191).

The list of the autorequired modules is below.

* clock
* compat
* config
* datetime
* decimal
* ffi
* fiber
* fio
* fun
* json
* log
* msgpack
* popen
* uuid
* varbinary
* yaml

See #9986 for motivation behind this feature.

This list forms so called initial environment for an interactive console
session. The default initial environment may be adjusted by an
application, for example, to include application specific administrator
functions.

Two public functions are added for this purpose: `console.initial_env()`
and `console.set_initial_env(env)`.

Example 1 (keep autorequired modules, but add one more variable):

```lua
local console = require('console')

-- Add myapp_info function.
local initial_env = console.initial_env()
initial_env.myapp_info = function()
    <...>
end
```

Example 2 (replace the whole initial environment):

```lua
local console = require('console')

-- Add myapp_info function, discard the autorequired modules.
console.set_initial_env({
    myapp_info = function()
        <...>
    end,
})
```

The `console.set_initial_env()` call without an argument or with a `nil`
argument drops the initial environment to its default.

A modification of the initial environment doesn't affect existing
console sessions. It affects console sessions that are created
after the modification.

Please, adjust the `console_session_scope_vars` compat option
description and extend the built-in `console` module reference with the
new functions.
@Totktonada Totktonada added the 3.2 Target is 3.2 and all newer release/master branches label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.2 Target is 3.2 and all newer release/master branches console feature A new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants