com-lihaoyi / Ammonite

Scala Scripting
http://ammonite.io
MIT License
2.6k stars 367 forks source link

unable to access package-private symbols from .sc files #1392

Open faucct opened 8 months ago

faucct commented 8 months ago

The execution model says that the script files are wrapped in a package object – https://ammonite.io/#ExecutionModel

Otherwise, the source code for this script is then wrapped in a package/object wrapper, corresponding to the path to the script from the current script's enclosing folder. For example, a script at path foo/bar/baz/Qux.sc will be wrapped in:

package foo.bar.baz
object Qux{
// script code
}

Despite this, I am unable to access the package-private symbols from the same package:

// org/apache/spark/sql/Foo.sc
import org.apache.spark.sql.Dataset

def foo(): Unit = {
    println(Dataset)
    println("foo")
}

def bar(): Unit = {
    println(Dataset)
}

almond jupyter kernel's cell:

import $file.org.apache.spark.sql.Foo

Foo.foo()

execution error:

Compiling /home/jovyan/shared/users/faucct/org/apache/spark/sql/Foo.sc
/home/jovyan/shared/users/faucct/org/apache/spark/sql/Foo.sc:4: object Dataset in package sql cannot be accessed in package org.apache.spark.sql
    println(Dataset)
            ^
/home/jovyan/shared/users/faucct/org/apache/spark/sql/Foo.sc:9: not found: value Dataset
    println(Dataset)
            ^
Compilation Failed

Though, I am not sure if the problem is on your side or Almond's. Putting the file three directories higher also does not help.

faucct commented 8 months ago

I guess the real package name gets prefixed by ammonite:

https://github.com/com-lihaoyi/Ammonite/blob/main/amm/compiler/src/main/scala/ammonite/compiler/DefaultCodeWrapper.scala#L20 https://github.com/com-lihaoyi/Ammonite/blob/main/amm/util/src/main/scala/ammonite/util/Util.scala#L100 https://github.com/com-lihaoyi/Ammonite/blob/89836cd882860fde0b2c6c62a562645e45f9d174/amm/interp/src/main/scala/ammonite/interp/Interpreter.scala#L236

It would be nice to make it customizable or even disable it.